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

为君一笑 提交于 2019-12-19 16:51:42

问题


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?


回答1:


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.




回答2:


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.




回答3:


MongoDB driver has a feature where if a collection does not already exist then it automatically creates one. you can also use umongo an equivalent of Sql server management studio. This video explains a clear sample to start off with https://www.youtube.com/watch?v=6x0-vHHHpv8



来源:https://stackoverflow.com/questions/16268654/how-to-create-a-new-database-in-mongodb-using-the-c-sharp-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!