我无法使用Scala驱动程序将数据保存到Mongo中
简而言之,代码已完成,但是数据从未插入Mongo。创建并订阅了观察者,但是什么也没有发生。
这是我的代码的副本
object MongoTest {
def main(args: Array[String]) {
val SERVER = "127.0.0.1"
val DATABASE="mytest"
val connection = MongoClient("mongodb://"+SERVER)
val database = connection.getDatabase(DATABASE)
var items:List[Document] = List[Document]()
for(i <- 1 to 10){
items = items:+ Document("_id"->new ObjectId(),"val"->i)//generate dummy data
}
val latch = new CountDownLatch(1)
val db = database.getCollection("testInsert")
db.insertMany(items).subscribe(new Observer[Completed] {
override def onError(e: Throwable): Unit = {
println("Error")
e.printStackTrace()
}
override def onSubscribe(subscription: Subscription): Unit = {
println("Exporting")
}
override def onComplete(): Unit = {
println("Completed")
connection.close()
latch.countDown()
}
override def onNext(result: Completed): Unit = {
println("Next")
}
})
latch.await()
}
}
我的程序的输出是:
INFO [main] (SLF4JLogger.java:71) - Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
DEBUG [main] (SLF4JLogger.java:56) - Updating cluster description to {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING}]
INFO [cluster-ClusterId{value='57ab00600aa9452d90826eb8', description='null'}-127.0.0.1:27017] (SLF4JLogger.java:71) - Opened connection [connectionId{localValue:1, serverValue:2}] to 127.0.0.1:27017
DEBUG [cluster-ClusterId{value='57ab00600aa9452d90826eb8', description='null'}-127.0.0.1:27017] (SLF4JLogger.java:56) - Checking status of 127.0.0.1:27017
INFO [cluster-ClusterId{value='57ab00600aa9452d90826eb8', description='null'}-127.0.0.1:27017] (SLF4JLogger.java:71) - Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 8]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=1163475}
DEBUG [cluster-ClusterId{value='57ab00600aa9452d90826eb8', description='null'}-127.0.0.1:27017] (SLF4JLogger.java:56) - Updating cluster description to {type=STANDALONE, servers=[{address=127.0.0.1:27017, type=STANDALONE, roundTripTime=1.2 ms, state=CONNECTED}]
Exporting
当我的程序正在运行(或挂起)时,mongo表示我的程序已连接,但未db.currentOp(1)
显示任何挂起的写操作。
我也尝试将writeConcern设置为Acknowledged和W1,但这似乎也不起作用。
我也正在使用mongod版本3.2.6和mongo-scala-driver版本1.0.1
我设法得到它到底工作用bulkWrite
在GitHub上和下面的例子为驾驶员这里
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句