Skip to content

Filter API Reference ​

The Filter enum provides composable filter expressions for querying documents.

Installation ​

Add PocketDB to your Cargo.toml:

toml
[dependencies]
pocketdb = "0.1"

Or via the command line:

bash
cargo add pocketdb

Import ​

rust
use pocketdb::query::Filter;

Or use the filter! macro:

rust
use pocketdb::filter;

Comparison Operators ​

Filter::eq ​

rust
pub fn eq(field: &str, value: PocketValue) -> Self

Equality comparison.

rust
let f = Filter::eq("name", PocketValue::String("Alice".to_string()));

Filter::ne ​

rust
pub fn ne(field: &str, value: PocketValue) -> Self

Not-equal comparison.


Filter::gt ​

rust
pub fn gt(field: &str, value: PocketValue) -> Self

Greater-than comparison.


Filter::gte ​

rust
pub fn gte(field: &str, value: PocketValue) -> Self

Greater-than-or-equal comparison.


Filter::lt ​

rust
pub fn lt(field: &str, value: PocketValue) -> Self

Less-than comparison.


Filter::lte ​

rust
pub fn lte(field: &str, value: PocketValue) -> Self

Less-than-or-equal comparison.


Filter::in_list ​

rust
pub fn in_list(field: &str, values: Vec<PocketValue>) -> Self

Matches if the field value is in the given list.

rust
let f = Filter::in_list("color", vec![
    PocketValue::String("red".to_string()),
    PocketValue::String("blue".to_string()),
]);

Filter::nin ​

rust
pub fn nin(field: &str, values: Vec<PocketValue>) -> Self

Matches if the field value is NOT in the given list.

Logical Operators ​

Filter::and ​

rust
pub fn and(left: Filter, right: Filter) -> Self

Logical AND of two filters.

rust
let f = Filter::and(
    Filter::gt("age", PocketValue::Int(18)),
    Filter::eq("active", PocketValue::Bool(true)),
);

Filter::or ​

rust
pub fn or(left: Filter, right: Filter) -> Self

Logical OR of two filters.


Filter::not ​

rust
pub fn not(inner: Filter) -> Self

Logical NOT of a filter.


Filter::nor ​

rust
pub fn nor(left: Filter, right: Filter) -> Self

Logical NOR — neither condition matches.

Element Operators ​

Filter::exists ​

rust
pub fn exists(field: &str, should_exist: bool) -> Self

Checks whether a field exists.

rust
let f = Filter::exists("email", true);    // Field must exist
let f = Filter::exists("phone", false);   // Field must NOT exist

Filter::type_is ​

rust
pub fn type_is(field: &str, type_name: &str) -> Self

Checks the type of a field. Type names: "null", "bool", "int", "float", "string", "array", "object", "objectId", "binary".

String Operators ​

Filter::regex ​

rust
pub fn regex(field: &str, pattern: &str) -> Self

Regular expression match on a string field.


Filter::starts_with ​

rust
pub fn starts_with(field: &str, prefix: &str) -> Self

String starts with prefix.


Filter::ends_with ​

rust
pub fn ends_with(field: &str, suffix: &str) -> Self

String ends with suffix.


Filter::contains ​

rust
pub fn contains(field: &str, value: PocketValue) -> Self

Array contains element, or string contains substring.

Array Operators ​

Filter::all ​

rust
pub fn all(field: &str, values: Vec<PocketValue>) -> Self

Array contains all specified values.


Filter::size ​

rust
pub fn size(field: &str, size: usize) -> Self

Array has exactly the specified number of elements.

Utility ​

Filter::always_true ​

rust
pub fn always_true() -> Self

Matches any document.


Filter::matches ​

rust
pub fn matches(&self, doc: &Document) -> bool

Returns true if the given document matches this filter.

filter! Macro ​

rust
use pocketdb::filter;

// Simple equality
let f = filter! { "name" => == "Alice" };

// Multiple conditions (implicit AND)
let f = filter! { "age" => > 18, "active" => == true };

// Supported operators: ==, !=, >, >=, <, <=

Combining Filters ​

Filters are composable — build complex queries by nesting:

rust
let f = Filter::and(
    Filter::eq("active", PocketValue::Bool(true)),
    Filter::or(
        Filter::eq("role", PocketValue::String("admin".to_string())),
        Filter::gt("age", PocketValue::Int(25)),
    ),
);

Released under the MIT License. Built by Muhammad Fiaz.