2014-03-13 39 views
2

我最近创建了一个MongoDB副本集,但目前对我来说没用,因为我找不到在其中指定选项的方法。mongodb在连接到副本集时指定MongoOptions

我的旧代码如下所示:

MongoOptions options = new MongoOptions(); 
options.autoConnectRetry = true; 
options.connectionsPerHost = 10000; 

mongo = new Mongo("1.1.1.1:27017", options); 

我读的副本集的文件,但据认为只有这样,才能创建副本在Java中设置连接器使用MongoURI是如下

mongo = new Mongo(new MongoURI("mongodb://1.1.1.1,1.1.1.251,1.1.1.37")); 

这部分工作,因为现在我没有看到任何提及MongoOptions的方式。

我在MongoURI或Mongo中找不到解决方法来指定MongoOptions。

回答

4

MongoMongoOptions现在有点过时..

使用MongoClientMongoClientOptions代替。

从的javadoc:

您可以连接到使用Java驱动程序通过传递 ServerAddress列表中MongoClient构造一个副本集。例如:

MongoClient mongoClient = new MongoClient(Arrays.asList(
    new ServerAddress("localhost", 27017), 
    new ServerAddress("localhost", 27018), 
    new ServerAddress("localhost", 27019))); 
+1

一个例子与种子构件阵列会更完整。 –

+0

@NeilLunn补充说。 –

2

参见一个简单的例子下面,使用MongoClientOptions的:

MongoClientOptions options = MongoClientOptions.builder() 
    .connectionsPerHost(20) 
    .autoConnectRetry(true) 
    .build(); 

mongo = new MongoClient(Arrays.asList(
     new ServerAddress("localhost", 27017), 
     new ServerAddress("localhost", 27018)), 
    options);