Expand description


A thin ODM layer for MongoDB built upon the official Rust driver.

Main features:

  • A stronger API leveraging Rust type system
  • Data structure models are defined using the well-known serde serialization framework
  • Index support on top of the Database::run_command (index management is currently not implemented in the underlying driver)
  • Indexes synchronization
  • Additional compile-time checks for queries using macros and type associated to mongo operators (eg: And instead of “$and”)


use mongodm::{ToRepository, Model, CollectionConfig, Indexes, Index, IndexOption, sync_indexes};
use mongodm::mongo::{Client, options::ClientOptions, bson::doc};
use serde::{Serialize, Deserialize};
use std::borrow::Cow;
// field! is used to make sure at compile time that some field exists in a given structure
use mongodm::field;

struct UserCollConf;

impl CollectionConfig for UserCollConf {
    fn collection_name() -> &'static str {

    fn indexes() -> Indexes {
            .with(Index::new(field!(last_seen in User))) // field! macro can be used as well

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct User {
    username: String,
    last_seen: i64,

impl Model for User {
    type CollConf = UserCollConf;

let client_options = ClientOptions::parse("mongodb://localhost:27017").await?;
let client = Client::with_options(client_options)?;
let db = client.database("mongodm_wayk_demo");

// indexes are now synced in backend for user collection

let repository = db.repository::<User>(); // method provided by `ToRepository` trait

let user = User {
    username: String::from("David"),
    last_seen: 1000,
repository.insert_one(&user, None).await?;

// We make sure at compile time that `username` is a field of `User`
let fetched_user = repository.find_one(doc! { field!(username in User): "David" }, None).await?;
assert_eq!(fetched_user.unwrap(), user);

// f! is a shorter version of field!
use mongodm::f;
repository.find_one(doc! { f!(username in User): "David" }, None).await?.unwrap();

// With static operators for queries (prevent invalid queries due to typos)
use mongodm::operator::*;
    doc! { And: [
        { f!(username in User): "David" },
        { f!(last_seen in User): { GreaterThan: 500 } },
    ] },


pub use mongodb as mongo;
pub use mongodb::bson;


Static operators for queries to prevent invalid queries due to typos.

Contains everything you need to use MongODM.


Construct a bson::BSON value from a literal.

Construct a bson::Document value.

Shorthand for field!.

Statically check presence of field in a given struct and stringify it.

Helper to build aggregation pipelines. Return a Vec as expected by the aggregate function.


Represents an individual update operation for the bulk_update function.

Result of a bulk_update operation.

Individual update result of a bulk_update operation. Contains the generated id in case of an upsert.

Specify field to be used for indexing and options.

Collection of indexes. Provides function to build database commands.

Associate a mongodb::Collection and a specific Model.


Option to be used at index creation.

Index sort order (useful for compound indexes).


Define collection name, configuration and associated indexes.

MongODM-provided utilities functions on mongodb::Collection<M>.

Associate a collection configuration.

Utilities methods to get a Repository. Implemented for mongodb::Database.


Synchronize backend mongo collection for a given CollectionConfig.