mongodb
ModuleMongoDB: 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;pub struct MongoDbDialectThe MongoDB dialect, and its own state: update operators, pipeline stages, search stages and the index hint.
use utopia_query::builder::mongodb::UpdateOperation;pub struct UpdateOperationAn update operator with its fields (field => value).
use utopia_query::builder::mongodb::IndexHint;pub enum IndexHintThe index a query must use: a name, or a key pattern ({"name": 1}).
From.pub type MongoDb = Builder<MongoDbDialect>A MongoDB builder: compiles to operation documents (JSON).
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()]);