vibe.d beta banner
get vibe.d
0.10.1

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.

Constructors

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

Properties

NameTypeDescription
database[get] MongoDatabase
name[get] string

Methods

NameDescription
aggregate (pipeline) Calculates aggregate values for the data in a collection.
count (query)
countDocuments (filter, options) Returns the count of documents that match the query for a collection or view.
createIndex (keys, indexOptions, options) Convenience method for creating a single index. Calls createIndexes
createIndexes (models, options, file, line) Builds one or more indexes in the collection.
deleteAll (options) Deletes all documents in the collection. The returned result identifies how many documents have been deleted.
deleteImpl (queries, options, limits) Implementation helper. It's possible to set custom delete limits with this method, otherwise it's identical to deleteOne and deleteMany.
deleteMany (filter, options) Deletes all documents matching the query filter. The returned result identifies how many documents have been deleted.
deleteOne (filter, options) Deletes at most one document matching the query filter. The returned result identifies how many documents have been deleted.
distinct (fieldName, query, options) Returns an input range of all unique values for a certain field for records matching the given query.
drop () Removes a collection or view from the database. The method also removes any indexes associated with the dropped collection.
dropIndex (name, options) Drops a single index from the collection by the index name.
dropIndexes (options) Drops all indexes in the collection.
dropIndexes (names, options) Unofficial API extension, more efficient multi-index removal on MongoDB 4.2+
ensureIndex (field_orders, flags, expire_time)
ensureIndex (field_orders, flags, expireAfterSeconds)
estimatedDocumentCount (options) Returns the count of all documents in a collection or view.
find (query, returnFieldSelector, flags, num_skip, num_docs_per_chunk)
find (query, projection, options) Queries the collection for existing documents, limiting what fields are returned by the database. (called projection)
find (query, options) Queries the collection for existing documents.
find () Queries all documents of the collection.
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)
findOne (query, projection, options) Queries the collection for existing documents.
findOne (query, options) Queries the collection for existing documents.
insert (document_or_documents, flags) Inserts new documents into the collection.
insertMany (documents, options) Inserts the provided document(s). If a document is missing an identifier, one is generated automatically by vibe.d.
insertOne (document, options) Inserts the provided document(s). If a document is missing an identifier, one is generated automatically by vibe.d.
listIndexes () Returns an array that holds a list of documents that identify and describe the existing indexes on the collection.
remove (selector, flags) Removes documents from the collection.
replaceOne (filter, replacement, options) Replaces at most single document within the collection based on the filter.
update (selector, update, flags) Performs an update operation on documents matching 'selector', updating them with 'update'.
updateImpl (queries, documents, perUpdateOptions, options, mustBeDocument, mustBeModification) Implementation helper. It's possible to set custom per-update object options with this method, otherwise it's identical to replaceOne, updateOne and updateMany.
updateMany (filter, replacement, options) Updates all matching document within the collection based on the filter.
updateOne (filter, replacement, options) Updates at most single document within the collection based on the filter.

Aliases

NameDescription
getIndexes

Example

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.insertOne(Bson(["name": Bson("admin"), "password": Bson("secret")]));

	// short version using a string[string] AA that is automatically
	// serialized to Bson
	users.insertOne(["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.insertOne(jusr);
}

Example

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.insertOne(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.get.loginName);
}
Authors

Sönke Ludwig

Copyright

© 2012-2016 Sönke Ludwig

License

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