Rust

mongodb

Module

MongoDB: MongoDb, a builder that compiles to MongoDB operation documents (JSON) instead of SQL.

A select compiles to a find (filter, projection, sort, skip, limit) or, when it needs stages a find cannot express (grouping, joins, window functions, unions, subqueries, search, pipeline stages), to an aggregate pipeline. Writes compile to insertMany, updateMany, updateOne (upserts) and deleteMany. Values are bound as ? strings in the document, in order, as with the SQL builders.

Contents

use utopia_query::builder::mongodb::MongoDbDialect;
Rust
pub struct MongoDbDialect

The MongoDB dialect, and its own state: update operators, pipeline stages, search stages and the index hint.

use utopia_query::builder::mongodb::UpdateOperation;
Rust
pub struct UpdateOperation

An update operator with its fields (field => value).

use utopia_query::builder::mongodb::IndexHint;
Rust
pub enum IndexHint

The index a query must use: a name, or a key pattern ({"name": 1}).

Also implements From.
Rust
pub type MongoDb = Builder<MongoDbDialect>

A MongoDB builder: compiles to operation documents (JSON).

Rust
use utopia_query::Query;
use utopia_query::builder::MongoDb;

let statement = MongoDb::new()
    .from("users", "")
    .select(["name"])
    .filter([Query::greater_than("age", 18)])
    .sort_desc("age", None)
    .limit(10)
    .build()?;
assert_eq!(
    statement.query,
    r#"{"collection":"users","operation":"find","filter":{"age":{"$gt":"?"}},"projection":{"name":1,"_id":0},"sort":{"age":-1},"limit":10}"#
);
assert_eq!(statement.bindings, vec![18.into()]);