Syntax
Filter Syntax

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.

OperatorDescriptionExample
$gtGreater than{ "age": { "$gt": 18 } }
$ltLess than{ "age": { "$lt": 30 } }
$gteGreater or equal{ "age": { "$gte": 18 } }
$lteLess or equal{ "age": { "$lte": 30 } }
$eqEqual to{ "age": { "$eq": 25 } }
$neNot equal{ "age": { "$ne": 25 } }
$inMatches any value in a list{ "status": { "$in": ["active", "pending"] } }
$ninDoes not match values in a list{ "status": { "$nin": ["inactive", "archived"] } }

3: Logical Operators

Combine multiple conditions for more advanced filtering.

OperatorDescriptionExample
$andAll conditions are met{ "$and": [ { "age": { "$gte": 18 } }, { "status": "active" } ] }
$orAny condition is met{ "$or": [ { "age": { "$lt": 18 } }, { "status": "inactive" } ] }
$notNegates a condition{ "age": { "$not": { "$gte": 18 } } }
$norNone of the conditions are met{ "$nor": [ { "age": { "$lt": 18 } }, { "status": "active" } ] }

4: Array Filters

Query array fields with specialized operators.

OperatorDescriptionExample
$allMatches all specified values{ "tags": { "$all": ["python", "mongodb"] } }
$sizeMatches arrays of a size{ "tags": { "$size": 3 } }
$elemMatchMatches elements in an array{ "scores": { "$elemMatch": { "math": { "$gt": 90 } } } }
$existsChecks if a field exists{ "nickname": { "$exists": true } }

5: Regular Expressions

Match string fields using regular expressions.

OperatorDescriptionExample
$regexMatches a pattern in a string{ "name": { "$regex": "^A" } }
$optionsAdds regex options (e.g., case insensitive){ "name": { "$regex": "smith", "$options": "i" } }

6: Projection Filters

Control which fields are returned in the query results.

OperatorDescriptionExample
$includeInclude specific fields{ "_id": 0, "name": 1, "age": 1 }
$excludeExclude specific fields{ "password": 0 }

7: Text Search

Perform full-text searches on string fields.

OperatorDescriptionExample
$textMatches text using a search index{ "$text": { "$search": "mongodb" } }
$languageSpecify a language for text search{ "$text": { "$search": "data", "$language": "en" } }

8: Geospatial Filters

Query location-based data.

OperatorDescriptionExample
$geoWithinMatches points within a geometry{ "location": { "$geoWithin": { "$centerSphere": [ [ 50, 50 ], 10 ] } } }
$nearMatches points near a location{ "location": { "$near": { "$geometry": { "type": "Point", "coordinates": [ 50, 50 ] }, "$maxDistance": 5000 } } }

How can we improve this documentation?

Got question? Email us