0% completed
Comparison operators in MongoDB are used to compare the values of fields in documents against specified values. These operators allow you to filter documents based on conditions such as equality, inequality, greater than, less than, and membership in a set. Understanding how to use comparison operators effectively is essential for constructing powerful and flexible queries.
Here, we have list down all comparison operators.
Operator | Description |
---|---|
$eq | Matches values that are equal to a specified value. |
$ne | Matches values that are not equal to a specified value. |
$gt | Matches values that are greater than a specified value. |
$gte | Matches values that are greater than or equal to a specified value. |
$lt | Matches values that are less than a specified value. |
$lte | Matches values that are less than or equal to a specified value. |
$in | Matches any of the values specified in an array. |
$nin | Matches none of the values specified in an array. |
The general syntax for using comparison operators in MongoDB is:
db.collection.find( { <field>: { <operator>: <value> } } )
First, let's insert some documents into the users
collection to work with:
db.users.insertMany([ { name: "Alice", age: 25, email: "alice@example.com", scores: [85, 90, 88] }, { name: "Bob", age: 30, email: "bob@example.com", scores: [78, 82, 79] }, { name: "Charlie", age: 35, email: "charlie@example.com", scores: [92, 85, 90] }, { name: "David", age: 28, email: "david@example.com", scores: [88, 84, 89] }, { name: "Eve", age: 22, email: "eve@example.com", scores: [80, 86, 81] }, { name: "Frank", age: 30, email: "frank@example.com", scores: [75, 82, 78] }, { name: "Grace", age: 40, email: "grace@example.com", scores: [95, 91, 94] }, { name: "Heidi", age: 25, email: "heidi@example.com", scores: [85, 89, 87] } ])
This setup creates a users
collection with various documents containing name
, age
, and email
fields. We will use these documents for demonstrating the comparison operators in the subsequent sections.
The $eq
operator matches documents where the value of a field is equal to the specified value.
Query for a Specific Age
db.users.find({ age: { $eq: 30 } })
This command retrieves all documents where the age
field is equal to 30.
Query for a Specific Name
db.users.find({ name: { $eq: "Alice" } })
This command retrieves all documents where the name
field is "Alice".
The $ne
operator matches documents where the value of a field is not equal to the specified value.
Query for Age Not Equal to 30
db.users.find({ age: { $ne: 30 } })
This command retrieves all documents where the age
field is not equal to 30.
Query for Name Not Equal to "Alice"
db.users.find({ name: { $ne: "Alice" } })
This command retrieves all documents where the name
field is not "Alice".
The $gt
operator matches documents where the value of a field is greater than the specified value.
Query for Age Greater Than 30
db.users.find({ age: { $gt: 30 } })
This command retrieves all documents where the age
field is greater than 30.
Query for Scores Greater Than 85
db.users.find({ scores: { $gt: 85 } })
This command retrieves all documents where any value in the scores
array is greater than 85.
The $gte
operator matches documents where the value of a field is greater than or equal to the specified value.
Query for Age Greater Than or Equal to 30
db.users.find({ age: { $gte: 30 } })
This command retrieves all documents where the age
field is greater than or equal to 30.
Query for Scores Greater Than or Equal to 85
db.users.find({ scores: { $gte: 85 } })
This command retrieves all documents where any value in the scores
array is greater than or equal to 85.
The $lt
operator matches documents where the value of a field is less than the specified value.
Query for Age Less Than 30
db.users.find({ age: { $lt: 30 } })
This command retrieves all documents where the age
field is less than 30.
Query for Scores Less Than 85
db.users.find({ scores: { $lt: 85 } })
This command retrieves all documents where any value in the scores
array is less than 85.
The $lte
operator matches documents where the value of a field is less than or equal to the specified value.
Query for Age Less Than or Equal to 30
db.users.find({ age: { $lte: 30 } })
This command retrieves all documents where the age
field is less than or equal to 30.
Query for Scores Less Than or Equal to 85
db.users.find({ scores: { $lte: 85 } })
This command retrieves all documents where any value in the scores
array is less than or equal to 85.
The $in
operator matches documents where the value of a field matches any of the values specified in an array.
Query for Age in a List
db.users.find({ age: { $in: [25, 30, 35] } })
This command retrieves all documents where the age
field is either 25, 30, or 35.
Query for Names in a List
db.users.find({ name: { $in: ["Alice", "Bob"] } })
This command retrieves all documents where the name
field is either "Alice" or "Bob".
The $nin
operator matches documents where the value of a field does not match any of the values specified in an array.
Query for Age Not in a List
db.users.find({ age: { $nin: [25, 30, 35] } })
This command retrieves all documents where the age
field is not 25, 30, or 35.
Query for Names Not in a List
db.users.find({ name: { $nin: ["Alice", "Bob"] } })
This command retrieves all documents where the name
field is not "Alice" or "Bob".
.....
.....
.....