Skip to content
hugoware edited this page Sep 14, 2010 · 2 revisions

The MongoDatabase creates a connection to a Mongo server so you can manipulate, select and remove documents. Below is a typical example of creating a MongoDatabase.

//creates a database using a connection string
using (MongoDatabase database = new MongoDatabase(connectionString)) {
    //do work in here
}

Using A Connection String

A MongoDatabase allows you to provide the host, port, username, password and target information as part of the constructor. You can also use a connection string as well. The format uses ‘key=value;’ format. A connection string is not case sensitive, however, does not trim out white space from values.

host
the target address of the server. (ex. 192.168.56.101)

database
The name of the database to connect to.

port [optional]
The port number to connect using – defaults to 27017

username [optional]
The username to use when authenticating to the database

password [optional]
The password that is matches the username being provided

autoconnect [optional]
Should the database automatically open the connection when needed. Defaults to ‘true’

A full connection string might look something like the one shown below.

host=192.168.100.10;database=myDatabase;username=yewser;password=mypassword;

Using The MongoAdminDatabase

Some commands can only be run from the Admin database on the Mongo server. These commands are available in the MongoAdminDatabase, which is accessed the same way as a regular MongoDatabase.

//connect to the admin database
using (MongoAdminDatabase database = new MongoAdminDatabase(connectionString)) {

    //get the build info
    BuildInfoResult build = database.GetBuildInfo();
    Console.WriteLine(build.Version);

}

The database value is not required in a connection string for a MongoAdminDatabase. If the value is set then it will be ignored (but the database instance is still created)

Authentication and Connection

By default, a MongoDatabase will connect and authenticate the first time a request is sent to the database. If you turn off the ‘autoconnect’ option then the database command ‘Open’ must be called manually.

Additionally, if you do not have both a ‘username’ and ‘password’ value set for your database then the authenticate call will not be called.

MongoCollection versus MongoDatabase

Most commands on the MongoDatabase connect immediately to do their work while a MongoCollection uses a lazy approach to talking to the server. So for example, the Insert command will add a record immediately but a MongoCollection requires that you call SubmitChanges before the record is added.

Consider the following example…

//create the database instance
using (MongoDatabase database = new MongoDatabase(connectionString)) {

    //adds the record immediately
    database.Insert("users", new { name = "immediate" });

    //queue up a record in a collection
    //the record has NOT been added yet
    MongoCollection collection = database.GetCollection("users");
    collection.InsertOnSubmit(new { name = "lazy" });

    //call submit changes to add a record
    collection.SubmitChanges();

    //or just call it on the database which will
    //check for all loaded collections and submit 
    //any waiting changes
    database.SubmitChanges();

}

Clone this wiki locally