Skip to main content
nestrs-mongodb is the Rust analogue of NestJS’s @nestjs/mongoose. It gives you a typed MongoRepository<T> over a mongodb::Collection<T>, a Document derive that reads #[schema(...)] / #[prop(...)] attributes, and the same MongoModule::for_root / for_feature boot pattern.

Install

The mongo feature on the umbrella re-exports the nestrs-mongodb types so you can write nestrs::MongoModule / nestrs::MongoRepository and skip the direct dep. Add mongo-dns for mongodb+srv:// Atlas seed lists.

Boot

MongoModule implements nestrs_core::Module (registers and exports MongoService), so it enters the graph through DynamicModule::from_module. for_root / for_feature are static setters called once at boot — they are not themselves import expressions. For vault / env / ConfigService lookups, use the NestJS MongooseModule.forRootAsync analogue before NestFactory::create:

Define a schema

  • #[schema(collection = "users", timestamps)] — collection name override + opt-in to created_at / updated_at BSON DateTime fields.
  • #[prop(rename = "email_address", unique)] — per-field metadata. The derive parses every #[prop(...)] at compile time so typos fail the build, not a deploy.
  • Document::collection_name() returns "users".
If #[schema(collection = …)] is omitted, the derive defaults to the snake_case + plural of the struct ident: User"users", BlogPost"blog_posts".

Use the typed repository

MongoRepository<T> wraps mongodb::Collection<T> and gives you a typed CRUD surface that doesn’t require reaching for bson::doc! for the common cases:

Configuration

MongoOptions builder exposes the driver tuning knobs the average app actually reaches for. Pass it through MongoModule::for_root_with_options when you need more than a plain URI:
default_database("app") is what MongoService::default_database() reads; MongoModule::for_feature("app") is the same string and the two names should match in single-database apps.

Feature flags

  • default = [] — TLS via rustls, BSON compat-3-0-0 codec.
  • dns-resolvermongodb+srv:// Atlas-style seed lists (pulls hickory-*).
  • all — convenience feature for everything.

API surface

MongoRepository<T> CRUD methods

Filters and updates are typed aliases over bson::Document so any driver-level filter / update is reachable without depending on mongodb directly.