The sample code from @Robby works, but it does not return what your code expects; it returns interface objects. The C # driver has been updated to use Interface methods, as well as a number of asynchronous functions, so updating the code is probably a good idea.
A new way to get the database is - well, you no longer get the database. You get IMongoDatabase , which is the interface to the database. In addition, you will no longer have to work with MongoCollection (from the MongoDatabase object), when you get IMongoDatabase , you will work with IMongoCollection . Refactoring? You are betting! But it's worth it.
I would also recommend placing your default database in connection strings with Mongo URLs. That way, you can save hard-coded constants, such as the database name, from your code.
// Get your connection string -- use the URL format as in example below: // name="MongoConnectionStr" connectionString="mongodb://localhost/bookstore" var connectionString = ConfigurationManager.ConnectionStrings["MongoConnectionStr"].ConnectionString; var mongoUrl = MongoUrl.Create(connectionString); var client = new MongoClient(connectionString); // Use the Mongo URL to avoid hard-coding the database name. var db = new MongoClient(mongoUrl).GetDatabase(mongoUrl.DatabaseName); // books below is an IMongoCollection var books = db.GetCollection<Book>("Books");
source share