简体   繁体   中英

How do you manage connections to mongodb from C#?

How do you manage mongodb connection from c#? close after use(CRUD)? or put the connection open all the time?

to put things in perspective here is my code:

string connectionString = "mongodb://localhost";
MongoClient client = new MongoClient(connectionString);            
MongoServer server = client.GetServer();            
MongoDatabase database = server.GetDatabase("UsersDb");

MongoClient is added to the driver since 1.7, to manage replica set stuff. It's supposed to be singleton and is thread-safe. You can always get MongoServer from it.
You don't need to worry about closing connections. It's managed by the driver. Refer to the tutorial for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM