如何在casbah mongo scala驱动程序中添加MongoOient中的MongoOptions

时间:2016-06-30 11:05:55

标签: mongodb scala casbah

我想将mongoOptions添加到MongoClient基本上我想添加ConnectionPerHost值,默认值是10我想将它增加到20 但我在使用两种不同方式尝试的代码中出现错误

val SERVER:ServerAddress = {

val hostName=config.getString("db.hostname")

val port=config.getString("db.port").toInt
        new ServerAddress(hostName,port)
          }
val DATABASE:String   = config.getString("db.dbname")

方法1

val options=MongoClientOptions.apply( connectionsPerHost=20 )
val connectionMongo = MongoConnection(SERVER).addOption(options.getConnectionsPerHost)//returning Unit instead of MongoClient 
val collectionMongo = connectionMongo(DATABASE)("testdb")

在最后一行Unit does not take parameters

上收到错误

方法2

val mongoOption=MongoClientOptions.builder()
                .connectionsPerHost(20)
                .build();

MongoClientOptions.builder()

上收到错误
value builder is not a member of object com.mongodb.casbah.MongoClientOptions

-

我想将connectionsPerHost值设置为20,请帮助正确的方法执行此操作

1 个答案:

答案 0 :(得分:1)

这似乎有效。

val config = ConfigFactory.load();
val hostName = config.getString("db.hostname")
val port = config.getInt("db.port")

val server = new ServerAddress(hostName, port)
val database = config.getString("db.dbname")

val options = MongoClientOptions(connectionsPerHost = 20)
val connectionMongo = MongoClient(server, options) 
val collectionMongo = connectionMongo(database)("testdb")

请注意MongoConnection is deprecated