vibe.d beta banner
get vibe.d

Function connectMongoDB

Connects to a MongoDB instance.

If the host/port form is used, default settings will be used, which enable safe updates, but no fsync. By specifying a URL instead, it is possible to fully customize the settings. See for the complete set of options. Note that 'sslverifycertificate' is only present in some client bindings, including here.

Note that the returned MongoClient uses a vibe.core.connectionpool.ConnectionPool internally to create and reuse connections as necessary. Thus, the MongoClient instance can - and should - be shared among all fibers in a thread by storing in in a thread local variable.


MongoClient connectMongoDB(
  string host,
  ushort port

MongoClient connectMongoDB(
  string host_or_url

MongoClient connectMongoDB(
  MongoClientSettings settings


Authenticated connections are supported by using a URL connection string such as "mongodb://user:password@host". Note that the driver currently only supports the "MongoDB-CR" authentication mechanism. Since new MongoDB versions, starting with 3.0, default to the new "SCRAM-SHA-1" method, it is necessary to manually switch to the old method. See for more information.


// connecting with default settings:
auto client = connectMongoDB("");
auto users = client.getCollection("users");
// connecting using the URL form with custom settings
auto client = connectMongoDB("mongodb://localhost/?slaveOk=true");
// connecting with SSL encryption enabled and verification off
auto client = connectMongoDB("mongodb://localhost/?ssl=true&sslverifycertificate=false");


host Specifies the host name or IP address of the MongoDB server.
port Can be used to specify the port of the MongoDB server if different from the default one.
host_or_url Can either be a host name, in which case the default port will be used, or a URL with the mongodb:// scheme.
settings An object containing the full set of possible configuration options.


A new MongoClient instance that can be used to access the database.


Throws an exception if a mongodb:// URL is given and the URL cannot be parsed. An exception will not be thrown if called with a hostname and port.


Sönke Ludwig


© 2012-2013 RejectedSoftware e.K.


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