mongoclient mongoclient = new mongoclient( "host1" , 27017 );
2. you can specify themongoclienturi connection string.
mongoclient mongoclient = new mongoclient(new mongoclienturi("mongodb://host1:27017"));
连接到副本集
mongodb将自动发现主要和副本。
you can specify the members using themongoclienturi connection string:
指定两个副本集:
复制代码 代码如下:
mongoclient mongoclient = new mongoclient( new mongoclienturi("mongodb://host1:27017,host2:27017,host3:27017"));
至少有一个副本集和副本集选项的成员:
复制代码 代码如下:
mongoclient mongoclient = new mongoclient( new mongoclienturi( "mongodb://host1:27017,host2:27017,host3:27017/?replicaset=myreplicaset"));
你可以使用serveraddress指定副本集中的所有成员.
mongoclient mongoclient = new mongoclient(
arrays.aslist(new serveraddress("host1", 27017),
new serveraddress("host2", 27017),
new serveraddress("host3", 27017)));
mongoclient mongoclient = new mongoclient( "localhost" , 27017 );
连接多个 mongos 实例:
mongoclient mongoclient = new mongoclient( new mongoclienturi("mongodb://host1:27017,host2:27017"));
mongoclient mongoclient = new mongoclient(
arrays.aslist(new serveraddress("host1", 27017),
new serveraddress("host2", 27017)));
string user; // the user name
string database; // the name of the database in which the user is defined
char[] password; // the password as a character array
// ...
mongocredential credential = mongocredential.createcredential(user, database, password);
mongoclientoptions options = mongoclientoptions.builder().sslenabled(true).build();
mongoclient mongoclient = new mongoclient(new serveraddress("host1", 27017),
arrays.aslist(credential),
options);