Query Filtering with Operators
In MongoDB, filtering documents means finding only those records that match certain criteria. This is done using the find() method along with query operators like $eq, $gt, $lt, $in, $and, and $or.
MongoDB's filtering power lies in these operators, which allow you to match values, ranges, conditions, and complex logical patterns.
Inserting Sample Data
Let's start by inserting some documents into a students collection:
db.students.insertMany([
{ name: "Alice", age: 22, grade: "A" },
{ name: "Bob", age: 25, grade: "B" },
{ name: "Charlie", age: 20, grade: "A" },
{ name: "David", age: 23, grade: "C" },
{ name: "Eva", age: 21, grade: "B" }
]);
{
acknowledged: true,
insertedIds: [ObjectId("..."), ObjectId("..."), ...]
}
1. Filtering with $eq (Equal)
The $eq operator matches documents where the field value is equal to a specified value.
db.students.find({ grade: { $eq: "A" } });
{ name: "Alice", age: 22, grade: "A" }
{ name: "Charlie", age: 20, grade: "A" }
Explanation: This query finds students whose grade is exactly A.
2. Filtering with $gt and $lt (Greater Than / Less Than)
Use $gt (greater than) and $lt (less than) for numeric comparisons.
db.students.find({ age: { $gt: 21 } });
{ name: "Alice", age: 22, grade: "A" }
{ name: "Bob", age: 25, grade: "B" }
{ name: "David", age: 23, grade: "C" }
Explanation: This query returns students who are older than 21.
Q: Can we combine $gt and $lt to find values in a range?
A: Yes! Use both inside the same object:
db.students.find({ age: { $gt: 20, $lt: 24 } });
{ name: "Alice", age: 22, grade: "A" }
{ name: "David", age: 23, grade: "C" }
3. Filtering with $in (Match Any of Multiple Values)
The $in operator matches any value in a specified array.
db.students.find({ grade: { $in: ["A", "B"] } });
{ name: "Alice", age: 22, grade: "A" }
{ name: "Bob", age: 25, grade: "B" }
{ name: "Charlie", age: 20, grade: "A" }
{ name: "Eva", age: 21, grade: "B" }
Explanation: This query finds all students whose grade is either A or B.
4. Using $and (All Conditions Must Match)
The $and operator allows combining multiple conditions.
db.students.find({
$and: [
{ grade: "A" },
{ age: { $lt: 22 } }
]
});
{ name: "Charlie", age: 20, grade: "A" }
Explanation: This query returns students who have grade A and are younger than 22.
5. Using $or (Any One Condition Must Match)
The $or operator returns documents that match at least one condition.
db.students.find({
$or: [
{ age: { $lt: 21 } },
{ grade: "C" }
]
});
{ name: "Charlie", age: 20, grade: "A" }
{ name: "Eva", age: 21, grade: "B" }
{ name: "David", age: 23, grade: "C" }
Explanation: This query returns students who are either younger than 21 or have grade C.
Combining Operators in a Query
You can combine $and, $or, and comparison operators for complex filtering.
db.students.find({
$and: [
{ grade: { $in: ["A", "B"] } },
{ age: { $gt: 21 } }
]
});
{ name: "Alice", age: 22, grade: "A" }
{ name: "Bob", age: 25, grade: "B" }
Summary
$eq: Equal to$gt/$lt: Greater than / Less than$in: Value is in a list$and: All conditions must match$or: At least one condition must match
By combining these operators, you can write powerful and flexible queries to filter data just the way you need.
