Querying array elements with Mongo Querying array elements with Mongo mongodb mongodb

Querying array elements with Mongo


If you simply run the below query MongoDB is smart enough to figure out what you're trying to do.

{ ingredients: "apple" }

Mongo will see that ingredients is a list and only return documents that contain "apple" some where in that list.


From the documentation:

Note: Fields containing arrays match conditional operators, if only one item matches.

Therefore, the following query:

db.collection.find( { field: { $gt:0, $lt:2 } } );

Will match a document that contains the following field:

{ field: [-1,3] }


Why do people write scalable applications for smoothies?

db.find({"ingredients":{$in: "apple"}});