我尝试连接到服务器并获取数据库。它运行正常,但 VS2013 向我显示警告:
Warning 1 'MongoDB.Driver.MongoClientExtensions.GetServer(MongoDB.Driver.MongoClient)' is obsolete: 'Use the new API instead.
string connectionString = "mongodb://localhost:27017";
MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
MongoClient mongoClient = new MongoClient(settings);
var server = mongoClient.GetServer();
var db = server.GetDatabase("bookstore");
var bookCollection = db.GetCollection<Book>("Book");
有人可以帮我解决这个问题吗?感谢阅读。
最佳答案
MongoServer
类在 2.0.0 版中已弃用(参见 here )。您可以直接在 MongoClient
对象上调用 GetDatabase()
:
MongoClient mongoClient = new MongoClient(settings);
var db = mongoClient.GetDatabase("bookstore");
有关连接到 MongoDB 服务器、检索数据库等的更多文档可以在 reference documentation 中找到。 .
关于c# - MongoDB 获取服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29597574/