
Query Operators in MongoDB
Query Operators in MongoDB
MongoDB provides a powerful set of query operators that allow you to filter and match documents based on various conditions. These operators enable you to create complex queries and retrieve data based on criteria like comparison, logical operations, element presence, and more.
Common Query Operators
1. Comparison Operators
These operators allow you to perform comparisons between the field value and a given value.
$eq
: Matches documents where the field equals the specified value.db.users.find({ age: { $eq: 25 } })
$ne
: Matches documents where the field does not equal the specified value.db.users.find({ age: { $ne: 25 } })
$gt
: Matches documents where the field is greater than the specified value.db.users.find({ age: { $gt: 25 } })
$gte
: Matches documents where the field is greater than or equal to the specified value.db.users.find({ age: { $gte: 25 } })
$lt
: Matches documents where the field is less than the specified value.db.users.find({ age: { $lt: 25 } })
$lte
: Matches documents where the field is less than or equal to the specified value.db.users.find({ age: { $lte: 25 } })
$in
: Matches documents where the field value is in a given array.db.users.find({ age: { $in: [25, 30, 35] } })
$nin
: Matches documents where the field value is not in a given array.db.users.find({ age: { $nin: [25, 30, 35] } })
2. Logical Operators
These operators allow you to combine multiple conditions using logical operators.
$and
: Matches documents that satisfy all the specified conditions.db.users.find({ $and: [{ age: { $gt: 25 } }, { age: { $lt: 35 } }] })
$or
: Matches documents that satisfy at least one of the specified conditions.db.users.find({ $or: [{ age: { $lt: 25 } }, { age: { $gt: 40 } }] })
$nor
: Matches documents that fail all the specified conditions (opposite of$or
).db.users.find({ $nor: [{ age: { $lt: 25 } }, { age: { $gt: 40 } }] })
$not
: Negates a condition (opposite of a given query).db.users.find({ age: { $not: { $gte: 30 } } })
3. Element Operators
These operators match documents based on the presence or absence of fields.
$exists
: Matches documents that have a field present or not present.db.users.find({ email: { $exists: true } }) // Matches documents where 'email' field existsdb.users.find({ email: { $exists: false } }) // Matches documents where 'email' field does not exist
$type
: Matches documents where the field is of the specified BSON type.db.users.find({ age: { $type: "int" } }) // Matches documents where 'age' is of type integer
4. Array Operators
These operators help you query documents based on array values.
$all
: Matches documents where the field is an array that contains all specified values.db.users.find({ hobbies: { $all: ["reading", "travelling"] } })
$elemMatch
: Matches documents where at least one element in an array matches the specified query.db.users.find({ hobbies: { $elemMatch: { type: "sports", duration: { $gt: 10 } } } })
$size
: Matches documents where the field is an array with the specified number of elements.db.users.find({ hobbies: { $size: 3 } }) // Matches documents where 'hobbies' has 3 elements
5. Regular Expression Operator
$regex
: Matches documents where the field value matches a regular expression pattern.db.users.find({ name: { $regex: "^A" } }) // Matches documents where 'name' starts with 'A'
You can also add options to the regular expression, such as
i
for case-insensitive matching:db.users.find({ name: { $regex: "^a", $options: "i" } }) // Case-insensitive match for names starting with 'a'
6. Geospatial Query Operators
MongoDB provides operators to work with geospatial data (e.g., coordinates) and perform geospatial queries.
$near
: Matches documents that are within a certain distance of a point.db.locations.find({ location: { $near: { $geometry: { type: "Point", coordinates: [ -73.97, 40.77 ] }, $maxDistance: 1000 } }})
$geoWithin
: Matches documents with geometries that are within a specified geometry.db.locations.find({ location: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [...] } } }})
$geoIntersects
: Matches documents where a field intersects with a specified geometry.db.locations.find({ location: { $geoIntersects: { $geometry: { type: "Polygon", coordinates: [...] } } }})
7. Projection Operators
MongoDB also provides operators to control which fields should be returned in the query results.
$slice
: Limits the number of elements returned from an array.db.users.find({}).project({ hobbies: { $slice: 3 } }) // Returns the first 3 elements of the 'hobbies' array
$elemMatch
: Specifies that a single element in the array should be returned based on the specified criteria.db.users.find({}).project({ hobbies: { $elemMatch: { type: "sports" } } }) // Returns array elements that match the condition
Combining Query Operators
MongoDB queries often combine multiple operators to create more complex conditions. For example, to find users who are between the ages of 25 and 40 and whose name starts with the letter 'A', you can combine multiple operators:
db.users.find({ $and: [ { age: { $gte: 25, $lte: 40 } }, { name: { $regex: "^A" } } ]})
Conclusion
MongoDB's query operators give you powerful ways to retrieve and filter data based on specific conditions. Whether you need to perform simple queries, use comparison operators, combine multiple conditions logically, or work with arrays and geospatial data, these operators make it easy to construct precise and efficient queries.