使用连接字符串在 C# 中使用 MongoClientSettings 创建 MongoClient

Create MongoClient with MongoClientSettings in C# with connection string

我只是在寻找一种使用 MongoClientSettings 和提供的连接字符串来初始化 MongoClient 的方法。

在项目网站上找不到任何这样的例子。这些都是他们的例子:

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

这样你就可以做到:

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

并使用此命令创建 MongoClient

var client = new MongoClient(settings);