MongoCollection

Represents a single collection inside a MongoDB.

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.

Constructors

this
this(MongoClient client, string fullPath)
Undocumented in source.
this
this(MongoDatabase db, string name)
Undocumented in source.

Members

Functions

aggregate
Bson aggregate(ARGS pipeline)

Calculates aggregate values for the data in a collection.

count
ulong count(T query)

Counts the results of the specified query expression.

distinct
auto distinct(string key, Q query)

Returns an input range of all unique values for a certain field for records matching the given query.

drop
void drop()
Undocumented in source. Be warned that the author may not have intended to support it.
dropIndex
void dropIndex(string name)
Undocumented in source. Be warned that the author may not have intended to support it.
ensureIndex
void ensureIndex(const(Tuple!(string, int))[] field_orders, IndexFlags flags, Duration expire_time)
deprecated void ensureIndex(int[string] field_orders, IndexFlags flags, ulong expireAfterSeconds)

Creates or updates an index.

find
MongoCursor!(T, R, U) find(T query, U returnFieldSelector, QueryFlags flags, int num_skip, int num_docs_per_chunk)
MongoCursor!(T, R, typeof(null)) find(T query)
MongoCursor!(Bson, R, typeof(null)) find()

Queries the collection for existing documents.

findAndModify
Bson findAndModify(T query, U update, V returnFieldSelector)
Bson findAndModify(T query, U update)

Combines a modify and find operation to a single atomic operation.

findAndModifyExt
Bson findAndModifyExt(T query, U update, V options)

Combines a modify and find operation to a single atomic operation with generic options support.

findOne
auto findOne(T query, U returnFieldSelector, QueryFlags flags)
auto findOne(T query)

Queries the collection for existing documents.

insert
void insert(T document_or_documents, InsertFlags flags)

Inserts new documents into the collection.

remove
void remove(T selector, DeleteFlags flags)
void remove()

Removes documents from the collection.

update
void update(T selector, U update, UpdateFlags flags)

Performs an update operation on documents matching 'selector', updating them with 'update'.

Properties

database
MongoDatabase database [@property getter]
name
string name [@property getter]

Examples

import vibe.data.bson;
import vibe.data.json;
import vibe.db.mongo.mongo;

void test()
{
	MongoClient client = connectMongoDB("127.0.0.1");
	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"}`);
	users.insert(jusr);
}

Using the type system to define a document "schema"

import vibe.db.mongo.mongo;
import vibe.data.serialization : 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("127.0.0.1");
	MongoCollection users = client.getCollection("myapp.users");

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

	// 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": usr.id]);
	if (!qusr.isNull)
		logInfo("User: %s", qusr.loginName);
}

Meta