MongoDB Query Operators Overview
MongoDB query operators are used to filter documents based on specific conditions when using the find()
method. These operators help us write powerful queries similar to WHERE conditions in SQL.
Types of Query Operators
Query operators fall into several categories:
- Comparison Operators –
$eq
,$ne
,$gt
,$gte
,$lt
,$lte
- Logical Operators –
$and
,$or
,$not
,$nor
- Element Operators –
$exists
,$type
- Array Operators –
$in
,$nin
,$all
Comparison Operators
Let’s assume we have the following collection named students
:
db.students.insertMany([
{ name: "Alice", age: 22, score: 88 },
{ name: "Bob", age: 25, score: 76 },
{ name: "Charlie", age: 20, score: 95 },
{ name: "David", age: 23, score: 60 }
]);
$gt (greater than)
db.students.find({ score: { $gt: 80 } });
Output:
{ name: "Alice", age: 22, score: 88 } { name: "Charlie", age: 20, score: 95 }
Explanation: This query returns students with scores greater than 80.
$lt (less than)
db.students.find({ age: { $lt: 23 } });
Output:
{ name: "Charlie", age: 20, score: 95 } { name: "Alice", age: 22, score: 88 }
Explanation: Finds students whose age is less than 23.
$eq and $ne (equal and not equal)
db.students.find({ age: { $eq: 25 } });
Output:
{ name: "Bob", age: 25, score: 76 }
db.students.find({ name: { $ne: "Alice" } });
Output:
{ name: "Bob", age: 25, score: 76 } { name: "Charlie", age: 20, score: 95 } { name: "David", age: 23, score: 60 }
Logical Operators
$and
db.students.find({
$and: [
{ age: { $gt: 21 } },
{ score: { $lt: 90 } }
]
});
Output:
{ name: "Alice", age: 22, score: 88 } { name: "Bob", age: 25, score: 76 } { name: "David", age: 23, score: 60 }
Explanation: Returns students older than 21 and with a score less than 90.
$or
db.students.find({
$or: [
{ age: { $lt: 21 } },
{ score: { $gt: 90 } }
]
});
Output:
{ name: "Charlie", age: 20, score: 95 }
Explanation: Returns students who are either under 21 or scored more than 90.
Intuition Check
Q: If we want to get students who are not 25 years old and scored more than 70, how should we write the query?
A: Use $ne
and $gt
inside $and
.
db.students.find({
$and: [
{ age: { $ne: 25 } },
{ score: { $gt: 70 } }
]
});
Element Operators
$exists
Returns documents that have a particular field.
db.students.find({ score: { $exists: true } });
Output:
All students will be returned because all documents contain the score field.
$type
Finds documents where the field is a specific BSON type (e.g., double, string, int).
db.students.find({ age: { $type: "int" } });
Array Operators
Let’s insert new documents for demonstrating array queries:
db.courses.insertMany([
{ title: "MongoDB Basics", tags: ["mongodb", "database", "nosql"] },
{ title: "React for Beginners", tags: ["react", "frontend"] },
{ title: "Node.js in Depth", tags: ["node", "backend", "javascript"] }
]);
$in
db.courses.find({ tags: { $in: ["nosql"] } });
Output:
{ title: "MongoDB Basics", tags: ["mongodb", "database", "nosql"] }
Explanation: Returns documents where the tags
array contains "nosql".
$all
Returns documents where the array contains all specified elements.
db.courses.find({ tags: { $all: ["mongodb", "nosql"] } });
Output:
{ title: "MongoDB Basics", tags: ["mongodb", "database", "nosql"] }
Summary
Query operators in MongoDB allow you to write expressive and powerful filters to retrieve exactly the data you need. With comparison, logical, and array operators, you can build almost any query your application requires.
In the next lesson, we’ll dive deeper into how to work with arrays and embedded documents using advanced queries.