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
148
votes
8 answers

$lookup on ObjectId's in an array

What's the syntax for doing a $lookup on a field that is an array of ObjectIds rather than just a single ObjectId? Example Order Document: { _id: ObjectId("..."), products: [ ObjectId("...."), ObjectId("..
Jason Lin
  • 1,957
  • 3
  • 16
  • 18
147
votes
9 answers

MongoDB: How to query for records where field is null or not set?

I have an Email document which has a sent_at date field: { 'sent_at': Date( 1336776254000 ) } If this Email has not been sent, the sent_at field is either null, or non-existant. I need to get the count of all sent/unsent Emails. I'm stuck at…
Andrew
  • 227,796
  • 193
  • 515
  • 708
138
votes
10 answers

Get the latest record from mongodb collection

I want to know the most recent record in a collection. How to do that? Note: I know the following command line queries works: 1. db.test.find().sort({"idate":-1}).limit(1).forEach(printjson); 2.…
inkriti
  • 1,605
  • 3
  • 14
  • 8
136
votes
3 answers

How to filter array in subdocument with MongoDB

I have array in subdocument like this { "_id" : ObjectId("512e28984815cbfcb21646a7"), "list" : [ { "a" : 1 }, { "a" : 2 }, { "a" : 3 }, { …
chenka
  • 1,463
  • 2
  • 10
  • 5
121
votes
2 answers

How can I use 'Not Like' operator in MongoDB

I can use the SQL Like Operator using pymongo, db.test.find({'c':{'$regex':'ttt'}}) But how can I use Not Like Operator? I tried db.test.find({'c':{'$not':{'$regex':'ttt'}}) but got error: OperationFailure: $not cannot have a regex
KyungHoon Kim
  • 2,859
  • 2
  • 23
  • 26
116
votes
11 answers

Does MongoDB's $in clause guarantee order

When using MongoDB's $in clause, does the order of the returned documents always correspond to the order of the array argument?
112
votes
5 answers

Searching by ObjectId on Mongo Compass

How does one use Mongo Compass and search by ObjectID? I've been searching for the documentation for this but haven't been successful with anything. I have tried: { "_id" : "58f8085dc1840e050034d98f" } { "$oid" : "58f8085dc1840e050034d98f" } {…
aamirl
  • 1,420
  • 2
  • 12
  • 18
111
votes
5 answers

How to execute update ($set) queries in MongoDB Compass tool?

I'm new to MongoDB Compass tool and am trying to update a field in my collection. Please can someone suggest where the update query must be written. Could find no options or panes in the tool to write custom queries be it selection / updation for…
Patrick
  • 1,635
  • 2
  • 13
  • 23
109
votes
5 answers

How to join multiple collections with $lookup in mongodb

I want to join more than two collections in MongoDB using the aggregate $lookup. Is it possible to join? Give me some examples. Here I have three collections: users: { "_id" : ObjectId("5684f3c454b1fd6926c324fd"), "email" :…
Siva M
  • 1,129
  • 2
  • 8
  • 7
106
votes
8 answers

Redirect output of mongo query to a csv file

I am using MongoDB 2.2.2 for 32-bit Windows7 machine. I have a complex aggregation query in a .js file. I need to execute this file on the shell and direct the output to a CSV file. I ensure that the query returns a "flat" json (no nested keys), so…
Aafreen Sheikh
  • 4,949
  • 6
  • 33
  • 43
105
votes
3 answers

Return only matched sub-document elements within a nested array

The main collection is retailer, which contains an array for stores. Each store contains an array of offers (you can buy in this store). This offers array has an array of sizes. (See example below) Now I try to find all offers, which are available…
Vico
  • 1,269
  • 2
  • 12
  • 16
104
votes
8 answers

Printing Mongo query output to a file while in the mongo shell

2 days old with Mongo and I have a SQL background so bear with me. As with mysql, it is very convenient to be in the MySQL command line and output the results of a query to a file on the machine. I am trying to understand how I can do the same with…
Parijat Kalia
  • 4,929
  • 10
  • 50
  • 77
99
votes
11 answers

MongoDB Full and Partial Text Search

Env: MongoDB (3.2.0) with Mongoose Collection: users Text Index creation: BasicDBObject keys = new BasicDBObject(); keys.put("name","text"); BasicDBObject options = new BasicDBObject(); options.put("name", "userTextSearch"); …
98
votes
7 answers

How to convert a pymongo.cursor.Cursor into a dict?

I am using pymongo to query for all items in a region (actually it is to query for all venues in a region on a map). I used db.command(SON()) before to search in a spherical region, which can return me a dictionary and in the dictionary there is a…
gladys0313
  • 2,569
  • 6
  • 27
  • 51
91
votes
5 answers

How to use MongoDBs aggregate `$lookup` as `findOne()`

So as you all know, find() returns an array of results, with findOne() returning just a simply object. With Angular, this makes a huge difference. Instead of going {{myresult[0].name}}, I can simply just write {{myresult.name}}. I have found that…
Fizzix
  • 23,679
  • 38
  • 110
  • 176