Mongodb scala 驱动程序不插入一个文档
Mongodb scala driver doesn't insertOne document
我尝试使用 scala 2.11.7 在 mongodb 3.0.7 中保存一个文档,下面是快速浏览:http://mongodb.github.io/mongo-scala-driver/1.0/getting-started/quick-tour/ 但是我 运行 这个例子没有任何反应。未创建数据库、集合和文档。
def main(args: Array[String]) {
println("Start")
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("mydb")
val collection: MongoCollection[Document] = database.getCollection("test");
val doc: Document = Document("_id" -> 0, "name" -> "MongoDB", "type" -> "database",
"count" -> 1, "info" -> Document("x" -> 203, "y" -> 102))
val observable: Observable[Completed] = collection.insertOne(doc)
observable.subscribe(new Observer[Completed] {
override def onNext(result: Completed): Unit = println("Inserted")
override def onError(e: Throwable): Unit = println("Failed")
override def onComplete(): Unit = println("Completed")
})
mongoClient.close()
println("End")
}
控制台:
Start
dic 13, 2015 3:05:16 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
dic 13, 2015 3:05:16 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Opened connection [connectionId{localValue:1, serverValue:6}] to localhost:27017
dic 13, 2015 3:05:16 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 7]}, minWireVersion=0, maxWireVersion=3, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=893710}
End
缺少什么?
我认为 mongoClient.close()
在 'Observable' 完成之前被调用。我想知道你如何用一种优雅的方式解决这个问题。但是关于这不是一个真正的应用程序,我认为你只是在测试一些。您可以将 Console.readLine() 放在 mongoClient.close
之前,这样当您看到 Inserted
时,您可以按任意键,应用程序将结束。
def main(args: Array[String]) {
println("Start")
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("mydb")
val collection: MongoCollection[Document] = database.getCollection("test");
val doc: Document = Document("_id" -> 0, "name" -> "MongoDB", "type" -> "database",
"count" -> 1, "info" -> Document("x" -> 203, "y" -> 102))
val observable: Observable[Completed] = collection.insertOne(doc)
observable.subscribe(new Observer[Completed] {
override def onNext(result: Completed): Unit = println("Inserted")
override def onError(e: Throwable): Unit = println("Failed")
override def onComplete(): Unit = println("Completed")
})
scala.Console.readLine()
mongoClient.close()
println("End")
}
我尝试使用 scala 2.11.7 在 mongodb 3.0.7 中保存一个文档,下面是快速浏览:http://mongodb.github.io/mongo-scala-driver/1.0/getting-started/quick-tour/ 但是我 运行 这个例子没有任何反应。未创建数据库、集合和文档。
def main(args: Array[String]) {
println("Start")
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("mydb")
val collection: MongoCollection[Document] = database.getCollection("test");
val doc: Document = Document("_id" -> 0, "name" -> "MongoDB", "type" -> "database",
"count" -> 1, "info" -> Document("x" -> 203, "y" -> 102))
val observable: Observable[Completed] = collection.insertOne(doc)
observable.subscribe(new Observer[Completed] {
override def onNext(result: Completed): Unit = println("Inserted")
override def onError(e: Throwable): Unit = println("Failed")
override def onComplete(): Unit = println("Completed")
})
mongoClient.close()
println("End")
}
控制台:
Start
dic 13, 2015 3:05:16 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
dic 13, 2015 3:05:16 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Opened connection [connectionId{localValue:1, serverValue:6}] to localhost:27017
dic 13, 2015 3:05:16 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMACIÓN: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 7]}, minWireVersion=0, maxWireVersion=3, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=893710}
End
缺少什么?
我认为 mongoClient.close()
在 'Observable' 完成之前被调用。我想知道你如何用一种优雅的方式解决这个问题。但是关于这不是一个真正的应用程序,我认为你只是在测试一些。您可以将 Console.readLine() 放在 mongoClient.close
之前,这样当您看到 Inserted
时,您可以按任意键,应用程序将结束。
def main(args: Array[String]) {
println("Start")
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("mydb")
val collection: MongoCollection[Document] = database.getCollection("test");
val doc: Document = Document("_id" -> 0, "name" -> "MongoDB", "type" -> "database",
"count" -> 1, "info" -> Document("x" -> 203, "y" -> 102))
val observable: Observable[Completed] = collection.insertOne(doc)
observable.subscribe(new Observer[Completed] {
override def onNext(result: Completed): Unit = println("Inserted")
override def onError(e: Throwable): Unit = println("Failed")
override def onComplete(): Unit = println("Completed")
})
scala.Console.readLine()
mongoClient.close()
println("End")
}