我正在练习一个需要数据库连接的项目,我正在使用 Play Framework 结合 Scala 和 MongoDB。
我也在使用 Mongo-scala-driver 并遵循文档。我写了完全相同的代码:
println("start")
val mongoClient: MongoClient = MongoClient("mongodb://localhost:27017/Sandbox")
val database: MongoDatabase = mongoClient.getDatabase("test")
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))
collection.insertOne(doc).subscribe(new Observer[Completed] {
override def onSubscribe(subscription: Subscription): Unit = println("Subscribed")
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
Subscribed
end
我一直在寻找类似主题的stackoverflow,但我发现的所有内容都对我不起作用。