Query Operators
Now that you know how to find documents with exact matches, it's time to level up! MongoDB provides powerful **query operators** that let you find documents based on conditions like greater than, less than, or matching values in arrays.
What are Query Operators?
Query operators are special keys that start with `$` and allow you to specify conditions in your queries. They make your searches much more powerful and flexible.
Think of query operators as special instructions like "find me products cheaper than $50" or "find users who are either 25 or 30 years old".
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
| `$gt` | Greater than | `{ price: { $gt: 100 } }` |
| `$gte` | Greater than or equal | `{ age: { $gte: 18 } }` |
| `$lt` | Less than | `{ stock: { $lt: 10 } }` |
| `$lte` | Less than or equal | `{ rating: { $lte: 3 } }` |
| `$eq` | Equal to | `{ status: { $eq: "active" } }` |
| `$ne` | Not equal | `{ status: { $ne: "deleted" } }` |
| `$in` | In array | `{ age: { $in: [25, 30, 35] } }` |
| `$nin` | Not in array | `{ category: { $nin: ["books", "toys"] } }` |
Examples with Comparison Operators
<!-- Find products cheaper than $50 -->db.products.find({ price: { $lt: 50 } }).pretty()
<!-- Find users aged 25 or 30 -->db.users.find({ age: { $in: [25, 30] } }).pretty()
<!-- Find expensive items (price >= 500) -->db.products.find({ price: { $gte: 500 } }).pretty()Logical Operators
Use `$and`, `$or`, `$not`, and `$nor` to combine multiple conditions.
| Operator | Description |
|---|---|
| `$and` | Matches if all conditions are true |
| `$or` | Matches if at least one condition is true |
Example with $or:
<!-- Find products that are either electronics OR cheaper than $20 -->db.products.find({ $or: [ { category: "electronics" }, { price: { $lt: 20 } } ]}).pretty()Example with $and (implied):
<!-- Electronics cheaper than $100 (implied AND) -->db.products.find({ category: "electronics", price: { $lt: 100 }})
<!-- Explicit AND (same result) -->db.products.find({ $and: [ { category: "electronics" }, { price: { $lt: 100 } } ]})Element Operators
| Operator | Description |
|---|---|
| `$exists` | Matches documents that have the specified field |
| `$type` | Matches if field is of specified type |
<!-- Find documents with an email field -->db.users.find({ email: { $exists: true } })
<!-- Find documents where age is a number -->db.users.find({ age: { $type: "number" } })Array Query Operators
| Operator | Description |
|---|---|
| `$size` | Matches arrays with specified number of elements |
| `$all` | Matches arrays that contain all specified elements |
<!-- Find posts with exactly 2 tags -->db.posts.find({ tags: { $size: 2 } })
<!-- Find posts tagged with both 'mongodb' AND 'nodejs' -->db.posts.find({ tags: { $all: ["mongodb", "nodejs"] } })Two Minute Drill
- Query operators start with `$` and add conditions to your queries.
- Comparison operators: `$gt`, `$lt`, `$gte`, `$lte`, `$in`, `$nin`.
- Logical operators: `$and`, `$or` for combining conditions.
- Element operators: `$exists`, `$type` for checking field existence and type.
- Array operators: `$size`, `$all` for querying arrays.
- These operators make MongoDB queries incredibly powerful and flexible.
Need more clarification?
Drop us an email at career@quipoinfotech.com
