Chapter 10: MongoDB Query Operators
1. What Exactly Are MongoDB Query Operators? (Big Picture)
Query operators are prefixed with $ keywords that tell MongoDB how to compare, match, combine, or evaluate field values.
Official categories (as per MongoDB 8.0 docs in 2026):
| Category | Purpose | Starts with $… examples | How often used (real projects) |
|---|---|---|---|
| Comparison | Compare values (>, <, =, in list, etc.) | $eq, $gt, $in, $ne | ★★★★★ (daily) |
| Logical | Combine multiple conditions (AND/OR/NOT) | $and, $or, $not, $nor | ★★★★☆ |
| Element | Check existence or type of fields | $exists, $type | ★★★☆☆ |
| Evaluation | JavaScript / regex / text / mod / jsonSchema | $regex, $where, $expr | ★★☆☆☆ (regex common) |
| Array | Work with arrays (contains, size, all, elemMatch) | $all, $elemMatch, $size | ★★★★☆ (very common) |
| Geospatial | Location-based queries (near, within polygon) | $geoWithin, $near, $nearSphere | ★★☆☆☆ (maps/apps) |
| Bitwise | Bit-level operations on integers | $bitsAllSet, $bitsAnyClear | ★☆☆☆☆ (rare, flags/status) |
| Comments | Add notes inside query (ignored by server) | $comment | ★☆☆☆☆ (debugging) |
→ Most apps use Comparison + Logical + Array 90% of the time.
2. Let’s Use Our Student Collection Again (Quick Reminder)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
use school2026 db.students.insertMany([ { name: "Rahul", age: 16, city: "Hyderabad", marks: {math: 92, science: 85}, hobbies: ["cricket", "coding"], isActive: true, joined: ISODate("2025-06-01") }, { name: "Priya", age: 15, city: "Secunderabad", marks: {math: 95}, hobbies: ["dance"], joined: ISODate("2025-07-15") }, { name: "Amit", age: 17, city: "Hyderabad", marks: {math: 68, physics: 72}, hobbies: ["kabaddi", "cricket"], joined: ISODate("2024-12-10") } ]) |
3. Hands-on – Most Important Operators with Examples
A. Comparison Operators (The Foundation – Learn These First!)
| Operator | Meaning | Example Query in mongosh | What it finds |
|---|---|---|---|
| $eq | Equal (same as plain: {age:16}) | { age: { $eq: 16 } } | Rahul |
| $gt | Greater than | { “marks.math”: { $gt: 90 } } | Rahul, Priya |
| $gte | ≥ | { age: { $gte: 16 } } | Rahul, Amit |
| $lt / $lte | < / ≤ | { age: { $lt: 16 } } | Priya |
| $ne | Not equal | { city: { $ne: “Hyderabad” } } | Priya |
| $in | Value in list | { city: { $in: [“Hyderabad”, “Bangalore”] } } | Rahul, Amit |
| $nin | Not in list | { hobbies: { $nin: [“cricket”] } } | Priya |
B. Logical Operators (Combine Conditions)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// AND (implicit when multiple fields) db.students.find({ age: { $gte: 16 }, city: "Hyderabad" }) // Rahul & Amit // Explicit $and (useful when same field multiple times) db.students.find({ $and: [ { "marks.math": { $gt: 80 } }, { "marks.math": { $lt: 95 } } ] }) // OR db.students.find({ $or: [ { city: "Secunderabad" }, { "marks.math": { $lt: 70 } } ] }) // Priya + Amit // NOT db.students.find({ age: { $not: { $gte: 16 } } }) // Priya |
C. Element Operators (Very Useful!)
|
0 1 2 3 4 5 6 7 8 9 10 |
// Field exists? db.students.find({ "marks.physics": { $exists: true } }) // Only Amit // Field is certain type db.students.find({ age: { $type: "number" } }) // All (but useful for mixed data) |
D. Array Operators (Super Common in Modern Apps)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Array contains value db.students.find({ hobbies: "cricket" }) // Rahul + Amit // Array contains ALL of these db.students.find({ hobbies: { $all: ["cricket", "coding"] } }) // Only Rahul // Array size exactly N db.students.find({ hobbies: { $size: 2 } }) // Rahul // $elemMatch – match condition inside array elements db.students.find({ marks: { $elemMatch: { math: { $gt: 90 }, science: { $exists: true } } } }) // Rahul (nested object treated like array in some cases, but here marks is object) |
E. Evaluation Operators (Powerful but Use Carefully)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
// Regular expression (like LIKE in SQL) db.students.find({ name: { $regex: "^P", $options: "i" } }) // Priya (starts with P, case-insensitive) // $expr – use aggregation expressions in query (compare two fields) db.students.find({ $expr: { $gt: ["$marks.math", 90] } }) |
F. Geospatial (If You Add Location Data)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Assume you add: location: { type: "Point", coordinates: [78.48, 17.38] } // Hyderabad approx db.students.createIndex({ location: "2dsphere" }) db.students.find({ location: { $near: { $geometry: { type: "Point", coordinates: [78.5, 17.4] }, $maxDistance: 10000 // 10 km } } }) |
4. Quick Summary Table – Your Cheat Sheet
| Category | Top 5 Operators You Must Know | Real-Life Example Use Case |
|---|---|---|
| Comparison | $eq, $gt/$gte, $lt/$lte, $in, $ne | Age filters, price ranges, status in [“active”,”pending”] |
| Logical | $and, $or, $not | (India OR USA) AND age > 18 |
| Array | $all, $elemMatch, $size, implicit contains | Tags include “urgent” & “billing”, skills list |
| Element | $exists, $type | Documents missing “emailVerified”, type checks |
| Evaluation | $regex, $expr | Search names, compare two fields in same doc |
5. Mini Exercise – Do It Right Now!
- Find students with math marks > 90 OR from Hyderabad
- Find students whose hobbies include BOTH “cricket” AND “coding”
- Find students who do NOT have “physics” marks
- Find students aged 16 to 17 (inclusive)
Understood everything so far? These operators are what make MongoDB queries feel like magic — flexible, expressive, and powerful.
Next class — what do you want?
- Deep dive into $elemMatch + array operators (very tricky but super useful)?
- Aggregation $match stage (same operators but in pipeline)?
- $regex patterns in detail?
- Or start combining them in a real mini-project (student search engine)?
Tell me — class is ready for more! 🚀❤️
Any operator confusing you? Ask right away — we’ll solve it together! 😄
