12

From the mongoDB.Driver docs (http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/)

Get a Reference to a Server Object

To get a reference to a server object from the client object, write this:

var server = client.GetServer();

In the latest release the GetServer method is gone, but the doc have not been updated, what do we use now?

Thanks for your time.

Community
  • 1
  • 1
MoonKnight
  • 23,214
  • 40
  • 145
  • 277

1 Answers1

15

GetServer is part of the old API.

To use the new, shiny and async-ready API simply call GetDatabase directly on the client to get an IMongoDatabase and GetCollection on it to get an IMongoCollection:

var db = client.GetDatabase("HamsterSchool");
var collection = db.GetCollection<Hamster>("Hamsters");
i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • Fair enough, thanks for the quick response. However, before I used to beable to test a connection via `mongoServer.State != MongoServerState.Connected`, now what can I do? Try to get a known database and check if what is returned is non-null? – MoonKnight Apr 05 '15 at 12:18
  • Also, `IMongoQuery` and `Query` have gone, do you know where to? – MoonKnight Apr 05 '15 at 12:19
  • @Killercam you can use `Mongoclient.ListDatabasesAsync` to make sure you're able to reach the server. – i3arnon Apr 05 '15 at 12:20
  • @Killercam Use the `Builders` class. – i3arnon Apr 05 '15 at 12:22
  • But not all permissions allow to list databases, or am I wrong there? I also assume that the `Query` stuff has now been replaced with the fluent syntax `var names = await db.GetCollection("people") .Find(x => x.FirstName == "Jack") .SortBy(x => x.Age) .Project(x => x.FirstName + " " + x.LastName) .ToListAsync();` etc. – MoonKnight Apr 05 '15 at 12:22
  • @Killercam That is more of a replacement for Linq. Query is what you used instead of the lambda expressions. – i3arnon Apr 05 '15 at 12:24
  • @Killercam I don't think there's any docs other than this post: https://www.mongodb.com/blog/post/introducing-20-net-driver – i3arnon Apr 05 '15 at 12:29
  • @Killercam take a look at this for checking for a connection: http://stackoverflow.com/q/29459990/885318 – i3arnon Apr 05 '15 at 17:17