Filter API Reference
The Filter enum provides composable filter expressions for querying documents.
Installation
Add PocketDB to your Cargo.toml:
[dependencies]
pocketdb = "0.1"Or via the command line:
cargo add pocketdbImport
use pocketdb::query::Filter;Or use the filter! macro:
use pocketdb::filter;Comparison Operators
Filter::eq
pub fn eq(field: &str, value: PocketValue) -> SelfEquality comparison.
let f = Filter::eq("name", PocketValue::String("Alice".to_string()));Filter::ne
pub fn ne(field: &str, value: PocketValue) -> SelfNot-equal comparison.
Filter::gt
pub fn gt(field: &str, value: PocketValue) -> SelfGreater-than comparison.
Filter::gte
pub fn gte(field: &str, value: PocketValue) -> SelfGreater-than-or-equal comparison.
Filter::lt
pub fn lt(field: &str, value: PocketValue) -> SelfLess-than comparison.
Filter::lte
pub fn lte(field: &str, value: PocketValue) -> SelfLess-than-or-equal comparison.
Filter::in_list
pub fn in_list(field: &str, values: Vec<PocketValue>) -> SelfMatches if the field value is in the given list.
let f = Filter::in_list("color", vec![
PocketValue::String("red".to_string()),
PocketValue::String("blue".to_string()),
]);Filter::nin
pub fn nin(field: &str, values: Vec<PocketValue>) -> SelfMatches if the field value is NOT in the given list.
Logical Operators
Filter::and
pub fn and(left: Filter, right: Filter) -> SelfLogical AND of two filters.
let f = Filter::and(
Filter::gt("age", PocketValue::Int(18)),
Filter::eq("active", PocketValue::Bool(true)),
);Filter::or
pub fn or(left: Filter, right: Filter) -> SelfLogical OR of two filters.
Filter::not
pub fn not(inner: Filter) -> SelfLogical NOT of a filter.
Filter::nor
pub fn nor(left: Filter, right: Filter) -> SelfLogical NOR — neither condition matches.
Element Operators
Filter::exists
pub fn exists(field: &str, should_exist: bool) -> SelfChecks whether a field exists.
let f = Filter::exists("email", true); // Field must exist
let f = Filter::exists("phone", false); // Field must NOT existFilter::type_is
pub fn type_is(field: &str, type_name: &str) -> SelfChecks the type of a field. Type names: "null", "bool", "int", "float", "string", "array", "object", "objectId", "binary".
String Operators
Filter::regex
pub fn regex(field: &str, pattern: &str) -> SelfRegular expression match on a string field.
Filter::starts_with
pub fn starts_with(field: &str, prefix: &str) -> SelfString starts with prefix.
Filter::ends_with
pub fn ends_with(field: &str, suffix: &str) -> SelfString ends with suffix.
Filter::contains
pub fn contains(field: &str, value: PocketValue) -> SelfArray contains element, or string contains substring.
Array Operators
Filter::all
pub fn all(field: &str, values: Vec<PocketValue>) -> SelfArray contains all specified values.
Filter::size
pub fn size(field: &str, size: usize) -> SelfArray has exactly the specified number of elements.
Utility
Filter::always_true
pub fn always_true() -> SelfMatches any document.
Filter::matches
pub fn matches(&self, doc: &Document) -> boolReturns true if the given document matches this filter.
filter! Macro
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:
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)),
),
);