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

17312 questions
90
votes
8 answers

MongoDB "root" user

Is there a super UNIX like "root" user for MongoDB? I've been looking at http://docs.mongodb.org/manual/reference/user-privileges/ and have tried many combinations, but they all seem to lack in an area or another. Surely there is a role that is…
No_name
  • 2,732
  • 3
  • 32
  • 48
88
votes
6 answers

Overflow sort stage buffered data usage exceeds internal limit

Using the code: all_reviews = db_handle.find().sort('reviewDate', pymongo.ASCENDING) print all_reviews.count() print all_reviews[0] print all_reviews[2000000] The count prints 2043484, and it prints all_reviews[0]. However when printing…
sheetal_158
  • 7,391
  • 6
  • 27
  • 44
84
votes
10 answers

How to exclude one particular field from a collection in Mongoose?

I have a NodeJS application with Mongoose ODM(Mongoose 3.3.1). I want to retrieve all fields except 1 from my collection.For Example: I have a collection Product Which have 6 fields,I want to select all except a field "Image" . I used "exclude"…
dany
  • 1,801
  • 7
  • 27
  • 40
83
votes
3 answers

What's the difference between replaceOne() and updateOne() in MongoDB?

MongoDB bulk operations have two options: Bulk.find.updateOne() Adds a single document update operation to a bulk operations list. The operation can either replace an existing document or update specific fields in an existing…
Mike
  • 14,010
  • 29
  • 101
  • 161
83
votes
1 answer

Mongodb find() query : return only unique values (no duplicates)

I have a collection of documents : { "networkID": "myNetwork1", "pointID": "point001", "param": "param1" } { "networkID": "myNetwork2", "pointID": "point002", "param": "param2" } { "networkID": "myNetwork1", …
Franckl
  • 1,401
  • 2
  • 12
  • 17
83
votes
6 answers

Conditional grouping with $exists inside $cond

I have two keys A and B and their existence in the document is mutually exclusive. I have to group by A when A exists and group by B when B exists. So I am $projecting the required value into a computed key called MyKey on which I'll perform a…
Aafreen Sheikh
  • 4,949
  • 6
  • 33
  • 43
81
votes
3 answers

How to get all the values that contains part of a string using mongoose find?

I have the following problem retrieving data from MongoDB using mongoose. Here is my Schema: const BookSchema = new Schema( { _id:Number, title:String, authors:[String], subjects:[String] } ); as you can…
Dyan
  • 1,703
  • 4
  • 19
  • 26
80
votes
16 answers

Get a count of total documents with MongoDB when using limit

I am interested in optimizing a "pagination" solution I'm working on with MongoDB. My problem is straight forward. I usually limit the number of documents returned using the limit() functionality. This forces me to issue a redundant query without…
randombits
  • 47,058
  • 76
  • 251
  • 433
79
votes
2 answers

MongoDB nested lookup with 3 levels

I need to retrieve the entire single object hierarchy from the database as a JSON. Actually, the proposal about any other solution to achieve this result would be highly appreciated. I decided to use MongoDB with its $lookup support. So I have three…
Yuriy
  • 1,384
  • 1
  • 11
  • 17
78
votes
3 answers

MongoDB C# Driver - Ignore fields on binding

When using a FindOne() using MongoDB and C#, is there a way to ignore fields not found in the object? EG, example model. public class UserModel { public ObjectId id { get; set; } public string Email { get; set; } } Now we also store a…
LiamB
  • 18,243
  • 19
  • 75
  • 116
77
votes
3 answers

MongoDB - The argument to $size must be an Array, but was of type: EOO / missing

Trying to create a MongoDB data source with icCube. The idea is to return the size of an array as a new field. Something like : $project: { "people": 1, "Count myFieldArray" : {$size : "$myFieldArray" } } But I'm getting for some records the…
ic3
  • 7,917
  • 14
  • 67
  • 115
76
votes
11 answers

MongoDB - Query on the last element of an array?

I know that MongoDB supports the syntax find{array.0.field:"value"}, but I specifically want to do this for the last element in the array, which means I don't know the index. Is there some kind of operator for this, or am I out of luck? EDIT: To…
Joseph Blair
  • 1,385
  • 2
  • 12
  • 25
76
votes
6 answers

How to stop insertion of Duplicate documents in a mongodb collection

Let us have a MongoDB collection which has three docs.. db.collection.find() { _id:'...', user: 'A', title: 'Physics', Bank: 'Bank_A' } { _id:'...', user: 'A', title: 'Chemistry', Bank: 'Bank_B' } { _id:'...', user: 'B', title: 'Chemistry',…
shashank
  • 947
  • 2
  • 7
  • 8
76
votes
5 answers

Update field in exact element array in MongoDB

I have a document structured like this: { _id:"43434", heroes : [ { nickname : "test", items : ["", "", ""] }, { nickname : "test2", items : ["", "", ""] }, ] } Can I $set the second element of the items array of the…
Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
74
votes
1 answer

MongoDB Find performance: single compound index VS two single field indexes

I'm looking for an advice about which indexing strategy to use in MongoDb 3.4. Let's suppose we have a people collection of documents with the following shape: { _id: 10, name: "Bob", age: 32, profession: "Hacker" } Let's…
Enrico Massone
  • 6,464
  • 1
  • 28
  • 56