Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB get server

I try to connect to server and get database. It run properly but it VS2013 show me a warning :

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");

Can someone help me solve this ? Tks for reading.

like image 868
Hana Avatar asked Apr 13 '15 03:04

Hana


2 Answers

The MongoServer class was deprecated in version 2.0.0 (see here). You can call GetDatabase() directly on the MongoClient object:

MongoClient mongoClient = new MongoClient(settings);
var db = mongoClient.GetDatabase("bookstore");

More documentation about connecting to the MongoDB server, retrieving a database, etc. can be found in the reference documentation.

like image 148
Robby Cornelissen Avatar answered Nov 14 '22 15:11

Robby Cornelissen


I changed my code to following:

var mongoUrl = new MongoUrl(connectionString);
var mongoClient = new MongoClient(mongoUrl);
MongoServer server = new MongoServer(MongoServerSettings.FromClientSettings(mongoClient.Settings));
like image 39
Juju Shen Avatar answered Nov 14 '22 13:11

Juju Shen