Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 367
Luke101 Avatar asked Apr 28 '13 22:04

Luke101


People also ask

Which command is used to create a database in MongoDB?

The use Command MongoDB use DATABASE_NAME is used to create database. The command will create a new database if it doesn't exist, otherwise it will return the existing database.

Can you use MongoDB with C#?

By developing with C# and MongoDB together one opens up a world of possibilities. Console, window, and web applications are all possible. As are cross-platform mobile applications using the Xamarin framework.

Does MongoDB automatically create database?

Unlike SQL, where we need to create a database, tables and then insert the values manually, MongoDB creates the database automatically. You just need to save a value in the defined collection with the preferred name. You don't even have to mention that you want to create a database.

Is there any need to create database command in MongoDB?

There is no create database command in MongoDB. Actually, MongoDB do not provide any command to create database. It may be look like a weird concept, if you are from traditional SQL background where you need to create a database, table and insert values in the table manually.


2 Answers

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.

like image 74
Davin Tryon Avatar answered Oct 16 '22 04:10

Davin Tryon


There is no specific API/command required to create a DB in mongo. It creates it automatically when required. Read this article. to understand this in detail.

like image 40
Aravind Yarram Avatar answered Oct 16 '22 04:10

Aravind Yarram