vibe.d beta banner
get vibe.d

Asynchronous I/O that doesn’t get in your way, written in D

Struct MongoCollection

Represents a single collection inside a MongoDB.

struct MongoCollection ;

All methods take arbitrary types for Bson arguments. serializeToBson() is implicitly called on them before they are send to the database. The following example shows some possible ways to specify objects.


this (client, fullPath)
this (db, name)


database[get] MongoDatabase
name[get] string


aggregate (pipeline) Calculates aggregate values for the data in a collection.
count (query) Counts the results of the specified query expression.
distinct (key, query) Returns an input range of all unique values for a certain field for records matching the given query.
drop ()
dropIndex (name)
ensureIndex (field_orders, flags, expire_time) Creates or updates an index.
find (query, returnFieldSelector, flags, num_skip, num_docs_per_chunk) Queries the collection for existing documents.
findAndModify (query, update, returnFieldSelector) Combines a modify and find operation to a single atomic operation.
findAndModifyExt (query, update, options) Combines a modify and find operation to a single atomic operation with generic options support.
findOne (query, returnFieldSelector, flags) Queries the collection for existing documents.
insert (document_or_documents, flags) Inserts new documents into the collection.
remove (selector, flags) Removes documents from the collection.
update (selector, update, flags) Performs an update operation on documents matching 'selector', updating them with 'update'.


import vibe.db.mongo.mongo;

void test()
	MongoClient client = connectMongoDB("");
	MongoCollection users = client.getCollection("myapp.users");

	// canonical version using a Bson object
	users.insert(Bson(["name": Bson("admin"), "password": Bson("secret")]));

	// short version using a string[string] AA that is automatically
	// serialized to Bson
	users.insert(["name": "admin", "password": "secret"]);

	// BSON specific types are also serialized automatically
	auto uid = BsonObjectID.fromString("507f1f77bcf86cd799439011");
	Bson usr = users.findOne(["_id": uid]);

	// JSON is another possibility
	Json jusr = parseJsonString(`{"name": "admin", "password": "secret"}`);


Using the type system to define a document "schema"

import vibe.db.mongo.mongo;
import : name;
import std.typecons : Nullable;

// Nested object within a "User" document
struct Address {
	string name;
	string street;
	int zipCode;

// The document structure of the "myapp.users" collection
struct User {
	@name("_id") BsonObjectID id; // represented as "_id" in the database
	string loginName;
	string password;
	Address address;

void test()
	MongoClient client = connectMongoDB("");
	MongoCollection users = client.getCollection("myapp.users");

	// D values are automatically serialized to the internal BSON format
	// upon insertion - see also
	User usr; = BsonObjectID.generate();
	usr.loginName = "admin";
	usr.password = "secret";

	// find supports direct de-serialization of the returned documents
	foreach (usr2; users.find!User()) {
		logInfo("User: %s", usr2.loginName);

	// the same goes for findOne
	Nullable!User qusr = users.findOne!User(["_id":]);
	if (!qusr.isNull)
		logInfo("User: %s", qusr.loginName);

Sönke Ludwig


© 2012-2016 RejectedSoftware e.K.


Subject to the terms of the MIT license, as written in the included LICENSE.txt file.