MongoDB Query Operators
MongoDB Query Operators 📘
1️⃣ Comparison Operators
Used to compare field values.
| Operator | Description |
|---|---|
$eq | Equal to |
$ne | Not equal to |
$gt | Greater than |
$gte | Greater than or equal |
$lt | Less than |
$lte | Less than or equal |
$in | Value in array |
$nin | Value not in array |
Example
2️⃣ Logical Operators
Used to combine multiple conditions.
| Operator | Description |
|---|---|
$and | All conditions true |
$or | Any condition true |
$not | Negates condition |
$nor | None of the conditions true |
Example
3️⃣ Element Operators
Used to check field existence or data type.
| Operator | Description |
|---|---|
$exists | Field exists or not |
$type | Field data type |
Example
4️⃣ Array Operators
Used for querying array fields.
| Operator | Description |
|---|---|
$all | Matches all array values |
$size | Array length |
$elemMatch | Match embedded array condition |
Example
5️⃣ Evaluation Operators
Used for expressions and pattern matching.
| Operator | Description |
|---|---|
$regex | Pattern matching |
$expr | Use expressions |
$mod | Modulo operation |
$text | Text search |
Example
6️⃣ Update Operators (Used in Update Queries)
| Operator | Purpose |
|---|---|
$set | Set field value |
$unset | Remove field |
$inc | Increment value |
$push | Add to array |
$pull | Remove from array |
Example
7️⃣ Projection Operators
Used to control returned fields.
SQL vs MongoDB Operators
| SQL | MongoDB |
|---|---|
= | $eq |
!= | $ne |
> | $gt |
< | $lt |
IN | $in |
LIKE | $regex |
Quick Recap 🧠
Operators start with
$Used in find, update, delete
Powerful & flexible
Replace complex SQL conditions
