Operator

Insert Operators

$lt and $lte

db.collection.find({'field' : {'$lt': 2}})
db.collection.find({'field' : {'$lte': 1}}) 

$gt and $gte

db.collection.find({'field' : {'$gt': 1}})
db.collection.find({'field' : {'$gte': 2}}) 

$ne

db.collection.find({'field' : {'$ne' : 'value'}}) 

$in and $nin

db.collection.find({'field' : {'$in' : ['element_1', 'element_2']}})
db.collection.find({'field' : {'$nin' : ['element_1', 'element_2']}})

$or

db.collection.find({"$or":[{'field_1' :'value'}, {'field_2' : 'value'}]}) 

$all

db.collection.find({ <field>: { $all: [ <value1>, <value2>, ... ] } })

$elemMatch

db.collection.find({"<array_field>":{"$elemMatch":{"<field>": "<value>"}}}) 

Query an Embedded Document or Array of Embedded Document

db.collection.find({"<array_field>.<field>": "<value>"})

$slice

db.collection.find({}, {"field":{"$slice": <limit>}})
db.collection.find({}, {"field":{"$slice":[<skip>, <limit>]}})

Update Operators

  • Two ways to manipulate values in arrays.

    • By position

    • Position operator (the $ character)

      • When we don’t know what index of the array to modify.

      • Updates only the first match.

    • Example

collection.update_one({<query>}, {"$inc" : {“comments.0.like” : 1}})
collection.update_many({<query>}, {"$inc" : {“comments.$.like” : 1}})

Last updated