https://docs.mongodb.com/manual/reference/operator/query/
{ : { $eq: } } db.inventory.find( { qty: { $eq: 20 } } ) The query is equivalent to:
db.inventory.find( { qty: 20 } )
We can query based on multiple criteria by passing in comma-separated queries.
db.potions.find( { "vendor": "Kettlecooked", "ratings.strength": 5 } )
We can use comparison query operators to match documents based on the comparison of a specified value. $gt greater than $gte $lt $lte Syntax: { field: { $lte: value} } Consider the following example which uses the $lt operator with a field from an embedded document:
db.inventory.update( { "carrier.fee": { $lte: 5 } }, { $set: { price: 9.99 } } ) This update() operation will set the price field value in the documents that contain the embedded document carrier whose fee field value is less than or equal to 5
$and https://docs.mongodb.com/manual/reference/operator/query/and/#op._S_and
Consider the following example:
db.inventory.find( { qty: { $exists: true, $nin: [ 5, 15 ] } } ) This query will select all documents in the inventory collection where the qty field exists and its value does not equal 5 or 15.
$ne greater than or equal to less than less than or equal to not equal to Common
db.potions.find({"price": {"$gt":10, "$lt": 20}}
db.potions.find( {"sizes" :{"$gt": 8, "$lt": 16} }
)
Each value in the array is checked individually. If at least 1 array value is true for each criteria,
the entire document matches.
{ : { $elemMatch: { , , ... } } }
The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria
find() takes a second parameter called a “projection” that we can use to specify the exact fields we want back by setting their value to true.
db.potions.find( {"grade": {"$gte": 80}}, {"vendor": true, "name": true} )
{ "_id": ObjectId(...), "vendor": "Kettlecooked", "name": "Shrinking" }
{"vendor": false, "price": false}
{"vendor": true, "price": true, "_id": false }
Whenever projecting, we either select or exclude the fields we want — we don’t do both.
"price is the field to sort db.potions.find().sort({"price": 1})
We can implement basic pagination by limiting and skipping over documents. To do this, we’ll use the skip() and limit() cursor methods.
db.potions.find().skip(3).limit(3)