How to create a new database in MongoDB using the c# driver

Luke101 picture Luke101 · Apr 29, 2013 · Viewed 22.1k times · Source

I have read through the mongodb documentation and cannot seem to find out how to create a new database. For example, in the documentation it says I can access the "test" database like this:

db.test.find()

Now what if I want to create my own database using syntax like this:

db.MyDB.find()

Also, is there any documentation I can read online I can further read about creating databases and collections using the DOS interface and the c# driver?

Answer

Davin Tryon picture Davin Tryon · Apr 29, 2013

I may be wrong, but buried in this documentation it looks like the call to GetDatabase on the server object will actually create the database if it has not already been created.

So, the C# line:

server.GetDatabase("myDB");

Will create a new database named myDB the first time it is called.


From the documentation:

GetDatabase maintains a table of MongoDatabase instances it has returned before, so if you call GetDatabase again with the same parameters you get the same instance back again.