Comparison Operators
Example documents:
{ name: "John", age: 30 }
{ name: "Jane", age: 25 }
{ name: "Bob", age: 40 }
You can use following to create these documents.
db.users.insertMany([{ name: "John", age: 30 },{ name: "Jane", age: 25 },{ name: "Bob", age: 40 }])
Query examples:
-
$eq
: find all documents where age is equal to 30
db.users.find({ age: { $eq: 30 } })
-
$ne
: find all documents where age is not equal to 30
db.users.find({ age: { $ne: 30 } })
-
$gt
: find all documents where age is greater than 30
db.users.find({ age: { $gt: 30 } })
-
$gte
: find all documents where age is greater than or equal to 30
db.users.find({ age: { $gte: 30 } })
-
$lt
: find all documents where age is less than 30
db.users.find({ age: { $lt: 30 } })
-
$lte
: find all documents where age is less than or equal to 30
db.users.find({ age: { $lte: 30 } })
Logical Operators
Example documents:
{ name: "John", age: 30, city: "New York" }
{ name: "Jane", age: 25, city: "Los Angeles" }
{ name: "Bob", age: 40, city: "New York" }
Query examples:
-
$and
: find all documents where age is greater than or equal to 30 and city is "New York"
db.users.find({ $and: [{ age: { $gte: 30 } }, { city: "New York" }] })
-
$or
: find all documents where age is less than 30 or city is "New York"
db.users.find({ $or: [{ age: { $lt: 30 } }, { city: "New York" }] })
-
$not
: find all documents where age is not equal to 30
db.users.find({ age: { $not: { $eq: 30 } } })
Element Operators
Example documents:
{ name: "John", age: 30, address: { city: "New York", state: "NY" } }
{ name: "Jane", age: 25 }
{ name: "Bob", age: 40, address: null }
Query examples:
-
$exists
: find all documents where the address field exists
db.users.find({ address: { $exists: true } })
-
$type
: find all documents where the address field is of type "object"
db.users.find({ address: { $type: "object" } })
Array Operators
Example documents:
{ name: "John", hobbies: ["reading", "hiking"] }
{ name: "Jane", hobbies: ["swimming", "yoga"] }
{ name: "Bob", hobbies: null }
Query examples:
-
$all
: find all documents where hobbies include both "reading" and "hiking"
db.users.find({ hobbies: { $all: ["reading", "hiking"] } })
-
$elemMatch
: selects documents if element(s) in an array field match the specified conditions.
db.inventory.find( {
sizes: { $elemMatch: { h: { $gt: 15 }, w: { $gt: 20 } } },
status: "A"
} )
-
$size
: selects documents if the array field is a specified size.
db.inventory.find( { tags: { $size: 3 } } )
Bitwise Operators
-
$bitsAllSet
: selects documents where all the specified bits are set.
db.users.find( { status: { $bitsAllSet: 16 } } )
-
$bitsAnySet
: selects documents where any of the specified bits are set.
db.users.find( { status: { $bitsAnySet: 1 } } )
Evaluation Operators
-
$expr
: allows the use of aggregation expressions within the query language.
db.sales.find( { $expr: { $gt: [ "$sales", "$goal" ] } } )
-
$mod
: performs a modulo operation on the value of a field and selects documents with a specified result.
db.inventory.find( { qty: { $mod: [ 4, 0 ] } } )
Top comments (0)