c#mongodb.driver 连接模式有哪些
客户端连接到服务端的3种模式:
1)连接到单实例 .
var client = new MongoClient("mongodb://config.net.cn:27017");
2) 连接到mongo集群(多节点)
var client = new MongoClient("mongodb://config.net.cn:27017,config.net.cn:27018,config.net.cn:27019");
3)连接到集群,且支持订阅,更多特性的连接方式。
var credentials = MongoCredential.CreateCredential(
databaseName: "BookAds",username: "账号",password: "密码"
);
var mongoClient = new MongoClient(new MongoClientSettings()
{
Server = new MongoServerAddress("mongodb://config.net.cn", 27022),
Credential = credentials,
ConnectionMode = ConnectionMode.Standalone,
ServerSelectionTimeout = TimeSpan.FromSeconds(15),
ClusterConfigurator = builder =>
{
builder.Subscribe(new SingleEventSubscriber<CommandStartedEvent>(CmdStartHandlerForFindCommand));//开始执行时的回调
builder.Subscribe(new SingleEventSubscriber<CommandSucceededEvent>(CmdSuccessHandlerForFindCommand));//执行成功后的回调
}
});
参考自 config.net.cn