Filter Operators
Comparison Operators
| Operator | Description | Example | Use Case |
|---|---|---|---|
$eq | Equals | { "age": { "$eq": 25 } } | Exact match |
$ne | Not equal | { "status": { "$ne": "inactive" } } | Exclude specific values |
$gt | Greater than | { "price": { "$gt": 100 } } | Minimum threshold |
$gte | Greater than or equal | { "rating": { "$gte": 4.0 } } | Minimum inclusive threshold |
$lt | Less than | { "quantity": { "$lt": 10 } } | Maximum threshold |
$lte | Less than or equal | { "discount": { "$lte": 0.5 } } | Maximum inclusive threshold |
$in | Matches any value in array | { "category": { "$in": ["electronics", "books"] } } | Multiple value selection |
$nin | Does not match any value in array | { "status": { "$nin": ["deleted", "banned"] } } | Exclude multiple values |
Logical Operators
| Operator | Description | Example | Use Case |
|---|---|---|---|
$and | Logical AND (all conditions true) | { "$and": [{"age": {"$gte": 18}}, {"status": "active"}] } | Multiple requirements |
$or | Logical OR (any condition true) | { "$or": [{"premium": true}, {"discount": {"$gt": 0.2}}] } | Alternative conditions |
$not | Logical NOT (condition false) | { "email": { "$not": { "$regex": "@spam.com" } } } | Negation |
$nor | Logical NOR (none of conditions true) | { "$nor": [{"deleted": true}, {"banned": true}] } | Exclude multiple conditions |
Array Operators
| Operator | Description | Example | Use Case |
|---|---|---|---|
$all | Contains all specified elements | { "tags": { "$all": ["premium", "verified"] } } | All elements must be present |
$elemMatch | Array element matches condition | { "orders": { "$elemMatch": { "amount": { "$gt": 100 } } } } | Complex array element filtering |
$size | Array size equals value | { "items": { "$size": 3 } } | Exact array length |
Text and Pattern Operators
| Operator | Description | Example | Use Case |
|---|---|---|---|
$regex | Regular expression match | { "name": { "$regex": "^John", "$options": "i" } } | Pattern matching |
$text | Full-text search | { "$text": { "$search": "mongodb tutorial" } } | Text search across indexed fields |
Existence and Type Operators
| Operator | Description | Example | Use Case |
|---|---|---|---|
$exists | Field exists | { "email": { "$exists": true } } | Check field presence |
$type | Field type check | { "price": { "$type": "number" } } | Data type validation |
Advanced Operators
| Operator | Description | Example | Use Case |
|---|---|---|---|
$expr | Expression-based queries | { "$expr": { "$gt": ["$spent", "$budget"] } } | Compare fields within document |
$jsonSchema | JSON Schema validation | { "$jsonSchema": { "required": ["name", "email"] } } | Document structure validation |
$mod | Modulo operation | { "quantity": { "$mod": [5, 0] } } | Divisibility checks |