MongoDB is one of the most commonly used NoSQL databases. Query operators and projection operators in MongoDB offer fine control over which documents are retrieved and what format to apply to the resulting data set.
We will shortly cover the different types of query and projection operators that are available within MongoDB. From filtering documents through to combining multiple conditions.
This reference will essentially describe all these operators through clear examples thus serving as a
quick ref or cheat-sheet.
For testing purposes you can use:
Dummy Data Set:聽https://gist.github.com/debojyotichatterjee9/3b134f18700aef047a7b50fa56713657
Mongo Playground:聽https://mongoplayground.net/p/SAIN3Opb3ci
Query Operators
Comparison Operators
Comparison operators in MongoDB are used to compare field values against a specified value. They are essential for filtering documents based on specific criteria.
$eq
(Equal)- Description: Matches documents where the field value is equal to the specified value.
- Example:
db.users.find({
"meta_data.age": {
$eq: 73
}
})
// Finds users who are exactly 73 years old.
$gt
(Greater Than)- Description: Matches documents where the field value is greater than the specified value.
- Example:
db.users.find({
"meta_data.age": {
$gt: 21
}
})
// Finds users who are older than 21.
$gte
(Greater Than or Equal)- Description: Matches documents where the field value is greater than or equal to the specified value.
- Example:
db.users.find({
"meta_data.age": {
$gte: 21
}
})
// Finds users who are 21 years old or older.
$in
(In)- Description: Matches documents where the field value is in the specified array.
- Example:
db.users.find({
username: {
$in: [
"rahul77",
"scotty76"
]
}
})
// Finds users with a username of either "rahul77" or "scotty76".
$lt
(Less Than)- Description: Matches documents where the field value is less than the specified value.
- Example:
db.users.find({
"meta_data.age": {
$lt: 30
}
})
// Finds users who are younger than 30.
$lte
(Less Than or Equal)- Description: Matches documents where the field value is less than or equal to the specified value.
- Example:
db.users.find({
"meta_data.age": {
$lt: 30
}
})
// Finds users who are 30 years old or younger.
$ne
(Not Equal)- Description: Matches documents where the field value is not equal to the specified value.
- Example:
db.users.find({
username: {
$ne: "danny.pfeffer"
}
})
// Finds users username status is not "danny.pfeffer".
$nin
(Not In)- Description: Matches documents where the field value is not in the specified array.
- Example:
db.users.find({
username: {
$nin: [
"danny.pfeffer",
"rahul77",
"scotty76"
]
}
})// Finds users whose username is neither "danny.pfeffer","rahul77","scotty76".
Logical Operators
Logical operators in MongoDB are used to combine multiple query clauses. They are powerful tools that allow you to build complex queries.
$and
(Logical AND)- Description: Joins query clauses with a logical AND, requiring all clauses to be true.
- Example:
db.users.find({
"$and": [
{
"meta_data.age": {
"$gt": 25
}
},
{
"username": "scotty76"
}
]
})
// Finds users who are older than 25 and have a username of "scotty76".
$or
(Logical OR)- Description: Joins query clauses with a logical OR, requiring at least one clause to be true.
- Example:
db.users.find({
"$or": [
{
"username": "scotty76"
},
{
"meta_data.age": {
"$lt": 30
}
}
]
})
// Finds users who have a username of "scotty76" or are younger than 30.
$not
(Logical NOT)- Description: Inverts the effect of a query expression, matching documents that do not meet the condition.
- Example:
db.users.find({
"meta_data.age": {
"$not": {
"$gt": 30
}
}
})
// Finds users who are not older than 30.
$nor
(Logical NOR)- Description: Joins query clauses with a logical NOR, requiring all clauses to be false.
- Example:
db.users.find({
"$nor": [
{
"username": "scotty76"
},
{
"meta_data.age": {
"$gt": 30
}
}
]
})
// Finds users who do not
// have a username of "scotty76" and are not older than 30.
Element Operators
Element operators are used to check the existence of fields and their types in documents.
$exists
(Field Exists)- Description: Matches documents that have the specified field.
- Example:
db.users.find({
"email": {
"$exists": true
}
})
// Finds users who have an email field.
$type
(Type)- Description: Selects documents if a field is of the specified BSON type.
- Example:
db.users.find({
"username": {
"$type": "string"
}
})
// Finds users where the username field is of type string.
Evaluation Operators
Evaluation operators allow you to perform calculations, validate data, or match fields against complex conditions.
$expr
(Evaluate a condition)- Description: Allows the use of aggregation expressions in the query language.
- Example:
db.users.find({
"$expr": {
"$gt": [
"$meta_data.enabled_on",
"$meta_data.dob"
]
}
})
// Finds users where enabled_on is greater than dob.
$jsonSchema
(JSON Schema Validation)- Description: Validates documents against the specified JSON schema.
- Example:
db.users.find({
"$jsonSchema": {
"bsonType": "object",
"required": [
"username",
"meta_data.age"
],
"properties": {
"username": {
"bsonType": "string"
},
"meta_data.age": {
"bsonType": "number",
"minimum": 0
}
}
}
})
// Finds users that match the JSON schema.
$mod
(Modulo)- Description: Performs a modulo operation on the value of a field and selects documents with a specified result.
- Example:
db.users.find({
"meta_data.age": {
$mod: [
4,
0
]
}
})
// Finds users items where age is divisible by 4.
$regex
(Regular Expression)- Description: Matches documents that satisfy a regular expression.
- Example:
db.users.find({
"name.first_name": {
$regex: "^Cand"
}
})
// Finds users whose name starts with "Cand".
$text
(Text Search)- Description: Performs a text search on the content of the fields indexed with a text index.
- Example:
db.users.find({
"$text": {
"$search": "Savannah"
}
})
// Finds users that contain the term "Savannah".
$where
(JavaScript expression)- Description: Matches documents that satisfy a JavaScript expression.
- Example:
db.users.find({
"$where": "this.meta_data.age > 30"
})
// Finds users who are older than 30 using a JavaScript expression.
Array Operators
Array operators allow you to perform operations on array fields within documents.
$all
(Match all elements in an array)- Description: Matches arrays that contain all the specified elements.
- Example:
db.users.find({
"dept": {
"$all": [
"development",
"hr"
]
}
})
// Finds users that have both "development" and "hr" dept.
$elemMatch
(Element Match)- Description: Matches documents that contain an array field with at least one element that matches all the specified criteria.
- Example:
db.users.find({
"address": {
"$elemMatch": {
"state": "New Jersey",
"country": "Austria"
}
}
})
// Finds users who has state New Jersey and Country Austria.
$size
(Array Size)- Description: Selects documents if the array field is a specified size.
- Example:
db.users.find({
"dept": {
"$size": 2
}
})
// Finds users that have exactly two dept.
Bitwise Operators
Bitwise operators are used to perform bit-level operations on numeric fields.
$bitsAllClear
(Bits All Clear)- Description: Matches documents where the specified bit positions are clear (i.e., 0).
- Example:
db.users.find({
"flags": {
"$bitsAllClear": 3
}
})
// Finds users where the first two bits of flags are clear.
$bitsAllSet
(Bits All Set)- Description: Matches documents where the specified bit positions are set (i.e., 1).
- Example:
db.users.find({
"flags": {
"$bitsAllSet": 5
}
})
// Finds users where the first and third bits of flags are set.
$bitsAnyClear
(Bits Any Clear)- Description: Matches documents where any of the specified bit positions are clear.
- Example:
db.users.find({
"flags": {
"$bitsAnyClear": 4
}
})
// Finds users where any of the specified bits are clear.
$bitsAnySet
(Bits Any Set)- Description: Matches documents where any of the specified bit positions are set.
- Example:
db.users.find({
"flags": {
"$bitsAnySet": 6
}
})
// Finds users where any of the specified bits are set.
Miscellaneous Operators
Miscellaneous operators are less commonly used but provide additional functionality in specific scenarios.
$comment
(Add comments to query)- Description: Attaches a comment to a query for later reference in logs and profiling.
- Example:
db.users.find({
"meta_data.age": {
"$gt": 25
}
}).comment("Finding users older than 25")
// Adds a comment to the query for logging or profiling purposes.
$rand
(Random Number)- Description: Matches documents in a randomized order.
- Example:
db.users.find({
"meta_data.age": {
"$gt": 25
}
}).sort({ "$rand": { } })
// Finds users older than 25 in a random order.
Projection Operators
Projection operators in MongoDB allow you to control which fields are returned in the query results. They help you include or exclude specific fields, project specific array elements, and even manipulate the data returned from the database.
- Include/Exclude Fields
1
(Include Field) and0
(Exclude Field)- Description: Use
1
to include a field and0
to exclude a field from the results. - Example:
db.users.find({},
{
"name.first_name": 1,
"email": 1,
"_id": 0
})
// Returns only the first_name and email fields, excluding the _id field.
$
(Positional Operator)- Description: Projects the first array element that matches the query condition.
- Example:
db.users.find({
"dept": {
"$eq": "hr"
}
},
{
"dept.$": 1
})
// Returns the first dept in the dept array that is equal to "hr".
$elemMatch
(Projection with Element Match)- Description: Projects the first matching element from an array.
- Example:
db.users.find({
"dept": {
"$elemMatch": {
"$eq": "management"
}
}
},
{
"dept": 1
})
// Projects the first dept in the dept array that is equal to "management".
$meta
(Metadata Projection)- Description: Projects metadata like text score or search highlights.
- Example:
db.users.find({
"$text": {
"$search": "MongoDB"
}
},
{
"score": {
"$meta": "textScore"
}
})
// Projects the text search score alongside the article data.
$slice
(Limit Array Elements)- Description: Limits the number of array elements displayed in the results.
- Example:
db.users.find({},
{
"dept": {
"$slice": 1
}
})
// Returns only the first dept from the dept array.
Final Thoughts
Understanding MongoDB’s query and projection operators is crucial for writing efficient and effective queries. By mastering these operators, you can filter, combine, and project data with precision, enabling you to retrieve exactly what you need from your MongoDB collections.
Whether you’re filtering documents, performing complex logical operations, or refining the output of your queries, these operators provide the flexibility and power necessary to handle a wide range of data retrieval scenarios.
As with all powerful tools, practice is key. Experiment with these operators in your MongoDB environment, and you’ll soon find yourself crafting more sophisticated and optimized queries.