Create MongoClient with MongoClientSettings in C# with connection string

2.4k Views Asked by At

I'm just looking for a way to initialize a MongoClient using MongoClientSettings with a provided connection string.

Can not find any example of this on the project website. These are all their examples:

// To directly connect to a single MongoDB server
// (this will not auto-discover the primary even if it's a member of a replica set)
var client = new MongoClient();

// or use a connection string
var client = new MongoClient("mongodb://localhost:27017");

// or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
var client = new MongoClient("mongodb://localhost:27017,localhost:27018,localhost:27019");
1

There are 1 best solutions below

0
On BEST ANSWER

This way you can do this :

MongoClientSettings settings = MongoClientSettings.FromConnectionString("ConnectionString");
// change some fields based on your needs
settings.WaitQueueTimeout = TimeSpan.FromMinutes(1);
settings.MinConnectionPoolSize = 100;
settings.MaxConnectionPoolSize = 500;

And create the MongoClient using this command:

var client = new MongoClient(settings);