Filter Syntax
CapybaraDB utilizes a filter syntax fully compatible with MongoDB, enabling flexible and intuitive querying of data.
1: Exact Match
Filter documents where a field equals a specific value.
{ "field": "value" }
2: Comparison Operators
Use comparison operators to match documents based on relative values.
Operator | Description | Example |
---|---|---|
$gt | Greater than | { "age": { "$gt": 18 } } |
$lt | Less than | { "age": { "$lt": 30 } } |
$gte | Greater or equal | { "age": { "$gte": 18 } } |
$lte | Less or equal | { "age": { "$lte": 30 } } |
$eq | Equal to | { "age": { "$eq": 25 } } |
$ne | Not equal | { "age": { "$ne": 25 } } |
$in | Matches any value in a list | { "status": { "$in": ["active", "pending"] } } |
$nin | Does not match values in a list | { "status": { "$nin": ["inactive", "archived"] } } |
3: Logical Operators
Combine multiple conditions for more advanced filtering.
Operator | Description | Example |
---|---|---|
$and | All conditions are met | { "$and": [ { "age": { "$gte": 18 } }, { "status": "active" } ] } |
$or | Any condition is met | { "$or": [ { "age": { "$lt": 18 } }, { "status": "inactive" } ] } |
$not | Negates a condition | { "age": { "$not": { "$gte": 18 } } } |
$nor | None of the conditions are met | { "$nor": [ { "age": { "$lt": 18 } }, { "status": "active" } ] } |
4: Array Filters
Query array fields with specialized operators.
Operator | Description | Example |
---|---|---|
$all | Matches all specified values | { "tags": { "$all": ["python", "mongodb"] } } |
$size | Matches arrays of a size | { "tags": { "$size": 3 } } |
$elemMatch | Matches elements in an array | { "scores": { "$elemMatch": { "math": { "$gt": 90 } } } } |
$exists | Checks if a field exists | { "nickname": { "$exists": true } } |
5: Regular Expressions
Match string fields using regular expressions.
Operator | Description | Example |
---|---|---|
$regex | Matches a pattern in a string | { "name": { "$regex": "^A" } } |
$options | Adds regex options (e.g., case insensitive) | { "name": { "$regex": "smith", "$options": "i" } } |
6: Projection Filters
Control which fields are returned in the query results.
Operator | Description | Example |
---|---|---|
$include | Include specific fields | { "_id": 0, "name": 1, "age": 1 } |
$exclude | Exclude specific fields | { "password": 0 } |
7: Text Search
Perform full-text searches on string fields.
Operator | Description | Example |
---|---|---|
$text | Matches text using a search index | { "$text": { "$search": "mongodb" } } |
$language | Specify a language for text search | { "$text": { "$search": "data", "$language": "en" } } |
8: Geospatial Filters
Query location-based data.
Operator | Description | Example |
---|---|---|
$geoWithin | Matches points within a geometry | { "location": { "$geoWithin": { "$centerSphere": [ [ 50, 50 ], 10 ] } } } |
$near | Matches points near a location | { "location": { "$near": { "$geometry": { "type": "Point", "coordinates": [ 50, 50 ] }, "$maxDistance": 5000 } } } |