我能够插入、读取和删除,但我无法使用 updateOne 或 updateMany 来修改文档。
我正在使用带有 libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "1.1.1" 的 MongoDB 3.2.7
def read() = {
val query = Document("title" -> "Text Tiling")
methods.find(query).subscribe(
(method: Document) => update(method.toJson()), // onNext
(error: Throwable) => Logger.debug(s"Query failed: ${error.getMessage}"), // onError
() => Logger.debug("onComplete") // onComplete
)}
def update(jsonSting:String): Unit = {
Logger.debug("update() " + jsonSting)
Logger.debug(methods.find().first().toString)
val observer = new Observer[UpdateResult] {
override def onSubscribe(subscription: Subscription): Unit = {
Logger.debug("onSubscribe: " + subscription.toString)
}
override def onComplete: Unit = {
Logger.debug("onComplete")
}
override def onError(e: Throwable): Unit = {
Logger.debug("onError: " + e.getMessage)
}
override def onNext(doc: UpdateResult) {
Logger.debug("onNext")
}
}
val filter = Document("title" -> "Text Tiling")
val mod = Document("$set" -> Document("reputation" -> 5))
val result = methods.updateOne(filter,mod).subscribe(observer)
Logger.debug("result: " + result)}
以下是日志:
[调试]应用程序-更新(){“_id”:{“$oid”:“5759542a4e0bf602adcab149”},“标题”:“文本平铺”,“声誉”:0}
[调试] 应用程序 - org.mongodb.scala.ObservableImplicits$BoxedObservable@61ddc581
[调试] 应用程序 - onSubscribe: org.mongodb.scala.ObservableImplicits$BoxedSubscription@6252b659
[调试] 应用程序 - 结果:()
[调试] 应用程序 - onComplete