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)

JavaScript

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)

JavaScript

C. Element Operators (Very Useful!)

JavaScript

D. Array Operators (Super Common in Modern Apps)

JavaScript

E. Evaluation Operators (Powerful but Use Carefully)

JavaScript

F. Geospatial (If You Add Location Data)

JavaScript

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!

  1. Find students with math marks > 90 OR from Hyderabad
  2. Find students whose hobbies include BOTH “cricket” AND “coding”
  3. Find students who do NOT have “physics” marks
  4. 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! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *