简体   繁体   中英

MongoServer does not contain a definition for 'Create'

I am getting the MongoServer does not contain a definition for 'Create' exception while compiling the below code. Please help me on this.

Libraries and versions

  • MongoDB.Bson 2.0.0
  • MongoDB.Driver 2.0.0
  • MongoDB.Driver.Core 2.0.0
List<Info> names = new List<Info>();
String name = "";
MongoServer server = MongoServer.Create(
    ConfigurationManager.AppSettings["connectionString"]);
MongoDatabase myDB = server.GetDatabase("ES");
MongoCollection<Info> Persons = myDB.GetCollection<Info>("MyCollection");
foreach (Info Aperson in Persons.FindAll())
{
    name = name + " " + Aperson.Name;
    names.Add(Aperson);
}

The MongoServer.Create() method was removed in version 2.0 . It has been deprecated since at least version 1.7 .

Instead, use MongoClient . To get access to the MongoServer , use MongoClient.GetServer()

MongoClient client = new MongoClient(
    ConfigurationManager.AppSettings["connectionString"]);
MongoServer server = client.GetServer();

This is what you need to do, using the new MongoDB.Driver 2.0:

var connectionString = ConfigurationManager.AppSettings["connectionString"];

var client = new MongoClient(connectionString);
var database = client.GetDatabase("ES");  

var collection = database.GetCollection<BsonDocument>("MyCollection");
var persons = await collection.Find(new BsonDocument()).ToListAsync();

See more at: https://www.mongodb.com/blog/post/introducing-20-net-driver

Regards.

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