Skip to content

MongoCrate

Stores items in a MongoDB collection. Production-ready with full query support, geospatial queries, text search, and ObjectId handling.

import crate.collection.mongo;
// From a collection object
auto products = new MongoCrate!Product(mongoCollection);
// From a client + collection name
auto products = new MongoCrate!Product(mongoClient, "products");
router.crateSetup!RestApi.add(products);

With config:

auto config = CrateConfig!Product();
config.deleteItem = false;
auto products = new MongoCrate!Product(mongoCollection, config);
  • Storage: MongoDB collection via vibe.d’s MongoDB driver
  • ID generation: Auto-generates BsonObjectID on insert
  • Queries: Native MongoDB queries via BsonQueryBuilder
  • Streaming: Results are streamed via InputRange!Json — large collections don’t load entirely into memory
  • ObjectId handling: Automatically converts string IDs to BsonObjectID for queries

Full MongoDB query capabilities through the IQuery interface:

crate.get()
.where("status").equal("active").and()
.where("tags").arrayContains("featured").and()
.where("position").near(longitude, latitude, 5000).and() // Geospatial
.sort("createdAt", -1)
.limit(50)
.skip(100)
.exec();

Supported query operators include equal, like, anyOf, greaterThan, lessThan, arrayContains, containsAll, insidePolygon, intersects, near, not, and or.

ISO date strings in query values are automatically converted to BsonDate for proper MongoDB date comparisons.

MongoCrate recursively processes nested structs, converting model references to their ID representation for storage and resolving them back on retrieval.