Questions tagged [mongodb-query]

This tag is for questions related to querying and updating MongoDB collections, either through the mongo shell or using a programming language driver.

This tag is for questions related to querying and updating MongoDB collections, either through the mongo shell or using a programming language driver.

Unlike most relational databases, MongoDB does not support SQL (Structured Query Language). Queries in MongoDB are expressed in the MongoDB Query Language which uses JSON from the mongo shell and BSON (Binary JSON) at the driver level.

MongoDB has a rich query language including many advanced operators as well as aggregation features such as the Aggregation Framework and Map-Reduce.

For effective query plans it is important to understand the indexing strategies and explain your slow queries to understand their index usage. By default MongoDB will log all queries slower than 100ms (a slowms value that can be adjusted either as a command-line option or within the mongo shell).

MongoDB also includes a Database Profiler which can be enabled to capture either slow queries, or all queries for a database.

Documentation

13762 questions
3
votes
1 answer

Mongodb: $exists and $ne doesn't work together

Say I have following documents in myCollection: /* 1 */ { "_id" : ObjectId("57861717ae5cdd68414b22fc"), "category" : " ", "name" : "Category is a space" } /* 2 */ { "_id" : ObjectId("57861728ae5cdd68414b22fd"), "category" :…
Nattyk
  • 137
  • 12
3
votes
2 answers

Find all documents where a sub-array element matches "nan"

I have a bunch of documents that look like this { "coordinates": [[1, 2], [nan, nan] ] } I would like to filter and find all documents where one of those arrays contains nan values like in the example.
iDontKnowBetter
  • 393
  • 1
  • 5
  • 19
3
votes
1 answer

Nested conditions in $cond aggregate

I'm trying to create a computed status field in my Mongo query (statuses: created, payment received, shipped, received, finished). db.orders.aggregate( [ { $project: { status: { $cond: { if: { $ne: ["$feedback", null] }, then:…
Asaf
  • 7,650
  • 17
  • 60
  • 109
3
votes
1 answer

Mongoose findAndUpdate() validators this null

I am trying to run validation on my schema before updating and here's the code for it. Schema var workSchema = mongoose.Schema({ location: { type: String, required: true, enum: LOCATIONS }, flags: { isHourly: { type:…
eugenekgn
  • 1,504
  • 15
  • 36
3
votes
2 answers

Adding a new field to 100 million records in mongodb

What is the fastest and safest strategy for adding a new field to over 100 million mongodb documents? Background Using mongodb 3.0 in a 3 node replica set We are adding a new field (post_hour) that is based on data in another field (post_time) in…
3
votes
1 answer

Return union of embedded arrays

I have a collection with documents, each document has an array. I want to get an array which is the result of the union of the document's embedded arrays. This is my collection: { "_id": ObjectId("...."), "filter": "a", "images": [ …
David Rojo
  • 1,983
  • 1
  • 15
  • 39
3
votes
0 answers

In MongoDB, how do I update a field with the value of an element inside an array?

I have data in one MongoDb table that I need copied to another MongoDb table. The first table has a field with different elements (see examples below) which I know how to work with. The second table, however, has a field with an array that is…
boilers222
  • 1,728
  • 7
  • 29
  • 55
3
votes
0 answers

mongodb remove array entries that are empty array - can't use $exists with ObjectId

I am trying to get the below query to work which I found in this post: Remove array entries containing an empty array However, I get the error Error: Can't use $exists with ObjectId. I am not sure if it is a version thing, I am using Mongo DB…
Onix
  • 31
  • 4
3
votes
1 answer

How to find and unset same sub field value on mongodb

I have 1 million documents in mongodb. I want to find and unset same fields. Can you give me a way or an idea? My documents like this: { "regions" : [ {"id" : "1", "name" : "World"}, {"id" : "10370","name" : "South…
tabarly
  • 41
  • 6
3
votes
1 answer

How to find the data from sub document in mongodb

I want to find the data which all belong to seller id,excluding other seller_id. For example :seller_id :1001, i want all the data belong to this seller id long with c_name,_id,c_order excluding seller_id:1000,1002 etc. { "_id" :…
Niraj Kumar
  • 38
  • 1
  • 5
3
votes
0 answers

How to query and sort data with 2 $in operators using only index covered query

I have a collection "activities" with model: { _id: ObjectId, action: String, userId: ObjectId, time: Date } I have a query: db.activities.find({ action: { $in: [ "1stAction", "2ndAction", ..., …
Andrey Hohutkin
  • 1,924
  • 1
  • 13
  • 17
3
votes
2 answers

Find field inside an array using $elemMatch

I have a invoice collection, in which I want find the document with a specified book's id. db.invoice.find({"sold": {$elemMatch: {"book":{$elemMatch:{"_id":"574e68e5ac9fbac82489b689"}}}}}) I tried this but it didn't work { "_id" :…
Ankit
  • 500
  • 4
  • 21
3
votes
2 answers

Calculate a score from an existing fields with conditions

I'm working on MongoDB 2.6.9 and NodeJs 0.10.37 and I have a collection vols which means flights. > db.vols.findOne() { "_id" : ObjectId("5717a5d4578f3f2556f300f2"), "Orig" : "AGP", "Dest" : "OTP", "Flight" : 126, "Routing" :…
Jean Dupont
  • 167
  • 10
3
votes
1 answer

Get the size of an array of each document in a sub array

I've a document in the collection 'gyms' which looks like the following: "name" : "Testing", "albums" : [ { "name" : "Aerobics", "photos" : [ { "filepath" : "aerobics.jpg" } ] …
Jamie
  • 2,823
  • 4
  • 30
  • 55
3
votes
1 answer

Using PyMongo, I need to fetch the fields of another collection

I need to construct a query, using PyMongo, which gets data from two related collections in a MongoDB database. Collection X has fields UserId, Name, and EmailId: [ { "UserId" : "941AB", "Name" : "Alex Andresson", "EmailId" : …
1 2 3
99
100