Query Operators in MongoDB
Query Operators in MongoDB
Query operators in MongoDB are used to specify conditions for matching documents in a collection. They allow you to perform complex queries and retrieve documents based on various criteria. MongoDB provides a rich set of query operators for different types of operations.
Here’s an overview of some commonly used query operators:
1. $eq (Equal To)
The $eq
operator matches documents where the value of a field is equal to the specified value.
Syntax:
db.collection.find({ field: { $eq: value } })
Example:
Find documents where the status
field is equal to "active"
:
db.users.find({ status: { $eq: "active" } })
This is equivalent to:
db.users.find({ status: "active" })
2. $ne (Not Equal To)
The $ne
operator matches documents where the value of a field is not equal to the specified value.
Syntax:
db.collection.find({ field: { $ne: value } })
Example:
Find documents where the age
field is not equal to 30
:
db.users.find({ age: { $ne: 30 } })
3. $gt (Greater Than)
The $gt
operator matches documents where the value of a field is greater than the specified value.
Syntax:
db.collection.find({ field: { $gt: value } })
Example:
Find documents where the age
field is greater than 25
:
db.users.find({ age: { $gt: 25 } })
4. $lt (Less Than)
The $lt
operator matches documents where the value of a field is less than the specified value.
Syntax:
db.collection.find({ field: { $lt: value } })
Example:
Find documents where the price
field is less than 100
:
db.products.find({ price: { $lt: 100 } })
5. $gte (Greater Than or Equal To)
The $gte
operator matches documents where the value of a field is greater than or equal to the specified value.
Syntax:
db.collection.find({ field: { $gte: value } })
Example:
Find documents where the age
field is greater than or equal to 30
:
db.users.find({ age: { $gte: 30 } })
6. $lte (Less Than or Equal To)
The $lte
operator matches documents where the value of a field is less than or equal to the specified value.
Syntax:
db.collection.find({ field: { $lte: value } })
Example:
Find documents where the price
field is less than or equal to 150
:
db.products.find({ price: { $lte: 150 } })
7. $in
The $in
operator matches documents where the value of a field is in the specified array of values.
Syntax:
db.collection.find({ field: { $in: [value1, value2, ...] } })
Example:
Find documents where the status
field is either "active"
or "pending"
:
db.orders.find({ status: { $in: ["active", "pending"] } })
8. $nin (Not In)
The $nin
operator matches documents where the value of a field is not in the specified array of values.
Syntax:
db.collection.find({ field: { $nin: [value1, value2, ...] } })
Example:
Find documents where the category
field is neither "electronics"
nor "furniture"
:
db.products.find({ category: { $nin: ["electronics", "furniture"] } })
9. $exists
The $exists
operator matches documents where a field exists or does not exist.
Syntax:
db.collection.find({ field: { $exists: true } })
Example:
Find documents where the email
field exists:
db.users.find({ email: { $exists: true } })
10. $regex
The $regex
operator matches documents where the value of a field matches a specified regular expression.
Syntax:
db.collection.find({ field: { $regex: pattern, $options: 'i' } })
- pattern: The regular expression pattern.
- $options: Optional flags (e.g.,
'i'
for case-insensitive).
Example:
Find documents where the name
field contains "john"
(case-insensitive):
db.users.find({ name: { $regex: /john/, $options: 'i' } })
11. $type
The $type
operator matches documents where the value of a field is of a specified BSON data type.
Syntax:
db.collection.find({ field: { $type: type } })
- type: The BSON data type (e.g.,
1
fordouble
,2
forstring
).
Example:
Find documents where the age
field is of type number
:
db.users.find({ age: { $type: 'number' } })