⬅ Previous Topic
Read Operations in MongoDBNext Topic ⮕
Update Operations in MongoDB⬅ Previous Topic
Read Operations in MongoDBNext Topic ⮕
Update Operations in MongoDBIn 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.
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("..."), ...] }
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
.
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" }
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
.
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.
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
.
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" }
$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 matchBy combining these operators, you can write powerful and flexible queries to filter data just the way you need.
⬅ Previous Topic
Read Operations in MongoDBNext Topic ⮕
Update Operations in MongoDBYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.