1

我能够插入、读取和删除,但我无法使用 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

4

2 回答 2

2

对订阅的调用请求对我有用。其他事件随后被调用。

override def onSubscribe(subscription: Subscription): Unit = {
    subscription.request(1)
  }

[调试] 应用程序 - onNext

[调试] 应用程序 - onComplete!

于 2016-06-09T21:00:38.077 回答
0
val filter : Bson = new Document(“key”, “old_value”) 
val newValue : Bson = new Document(“key” ,”new_value”)
val updateOperationDocument : Bson = new Document("$set", newValue)
val collection = db.getCollection(“collection_name”)
collection.updateOne(filter, updateOperationDocument)
于 2019-07-24T07:12:54.790 回答