Labs ICT
⭐ Pro Login

Query Operators

Advanced filtering with $gt, $lt, $in, and more.

MongoDB Query Operators

So you want to filter your data like a pro? Query operators are your best friend here. They let you go beyond simple equality checks and really dig into your data with precision.

Think of operators as the language you use to have a conversation with MongoDB. Instead of saying "find me this exact thing," you can say "find me anything greater than this" or "find me anything that matches this pattern."

Equality and Inequality

The $eq operator finds documents where a field equals a specific value. It's the default behavior when you use { field: value }, but using $eq explicitly makes your intent crystal clear.

On the flip side, $ne (not equals) helps you exclude specific values. It's like telling MongoDB, "Give me everything except this one thing."

db.users.find({ age: { $eq: 25 } })

db.users.find({ status: { $ne: "inactive" } })

db.users.find({ "address.city": { $eq: "New York" } })
Try it Yourself β†’

Range Queries

Need to find numbers within a range? $gt (greater than), $gte (greater than or equal), $lt (less than), and $lte (less than or equal) have you covered.

These are super useful for things like finding users in a certain age range, products within a price range, or logs from a specific time period. You can even combine them for complex range queries.

db.products.find({ price: { $gt: 10, $lt: 50 } })

db.users.find({ age: { $gte: 18, $lte: 35 } })

db.orders.find({ total: { $gte: 100 } })
Try it Yourself β†’

Membership Operators

The $in operator checks if a field's value matches any value in a specified array. It's like asking, "Is this value one of these options?" Super handy for filtering by multiple categories or statuses.

$nin (not in) is the oppositeβ€”it excludes documents where the field matches any value in the array. Perfect for filtering out certain categories or statuses.

db.users.find({ status: { $in: ["active", "pending"] } })

db.products.find({ category: { $nin: ["discontinued", "out_of_stock"] } })

db.users.find({ role: { $in: ["admin", "moderator"] } })
Try it Yourself β†’