# Filter Syntax Every filter is a JSON object. At its simplest form, a filter acts as a mask, checking if the values of the filter match those of the object being filtered. ```json { "key": "value", "nested": { "key": "value" } } ``` This filter would match against an object like: ```json { "key": "value", "other": "data", "nested": { "key": "value" } } ``` ## Advanced Filtering with Operators Operators enable complex filtering by allowing operations on keys and values. ### **Example**: Logical Chaining and Regex ```json { "$or": [ { "key": { "$regex": "^L" }}, { "key": { "$regex": "in$" }}, ] } ``` This filter would match all of these objects: ```json { "key": "Login" } { "key": "Lenin"} ``` but not: ```json { "key": "lol"} ``` ## Logical Operators ### `$and` Chain multiple filters together. All must evaluate to `true` ```json { "$and": [ { "key": "val" }, { "another": "filter" } ] } ``` ### `$or` Chain multiple filters together. At least one must evaluate to `true` ```json { "$or": [ { "key": "val" }, { "another": "filter" } ] } ``` ### `$not` Inverts the result of the nested filter expression. ```json { "$not": { "key": "value" }} ``` ## Comparison Operators ### `$lt` & `$lte` Evaluates to `true` if the value is less than or less than equal the specified value. ```json { "key": { "$lt": 5 }} ``` ### `$gt` & `$gte` Evaluates to `true` if the value is greater than or greater than equal the specified value. ```json { "key": { "$gt": 5 }} ``` ### `$ne` Evaluates to `true` if the value is not equal to the specified value. ```json { "key": { "$ne": "value" }} ``` > **Note**: The `$eq` operator is implicit. Example: `{ "key": "value" }` ## Array / Object Operators ### `$in` & `$contains` Evaluates to `true` if the value exists in the specified array. ```json { "array": { "$in": "value" }} ``` ### `$nin` Evaluates to `true` if the value does not exist in the specified array. ```json { "array": { "$nin": "value" }} ``` ### `$exists` Checks wether the key exists in the object. ```json {"key": { "$exists": true }} {"key": { "$exists": false }} ``` ### `$size` Evaluates to `true` if the array length matches the specified value ```json { "array": { "$size": 5 }} ``` ## Text Operators ### `$regex` Evaluates to `true` if the value matches the regular expression pattern. ```json { "key": { "$regex": "^regex" }} ``` ### `$contains` Evaluates to `true` if the value contains the text. ```json { "key": { "$contains": "text" }} ``` ### `$any` Evaluates to `true` if any value matches the filter. ```json { "array": { "$any": { "$regex": "text" }}} ``` ### `$all` Evaluates to `true` if all value matches the filter. ```json { "array": { "$all": { "$regex": "text" }}} ``` ## Misc Operators ### `$type` Evaluates to `true` if the value matches the specified type. ```json { "key": { "$type": "null" }} { "key": { "$type": "string" }} { "key": { "$type": "number" }} { "key": { "$type": "object" }} { "key": { "$type": "array" }} { "key": { "$type": "boolean" }} ``` ### `$range` Evaluates to `true` if the value is within the specified range. This operator is actually just syntactic sugar for `let filter = json!({"$and": [{"key": {"$gte": x}}, {"key": {"$lte": y}}]});`. ```json {"key": { "$range": [x, y] }} ```