Reputation: 358
I'm able to insert, read, and delete but I cannot get either updateOne or updateMany to modify documents.
I'm using MongoDB 3.2.7 with libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "1.1.1"
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)}
Here are the logs:
[debug] application - update() { "_id" : { "$oid" : "5759542a4e0bf602adcab149" }, "title" : "Text Tiling", "reputation" : 0 }
[debug] application - org.mongodb.scala.ObservableImplicits$BoxedObservable@61ddc581
[debug] application - onSubscribe: org.mongodb.scala.ObservableImplicits$BoxedSubscription@6252b659
[debug] application - result: ()
[debug] application - onComplete
Upvotes: 1
Views: 1832
Reputation: 31
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)
Upvotes: 0
Reputation: 358
Calling request on the subscription worked for me. The other events were then called after.
override def onSubscribe(subscription: Subscription): Unit = {
subscription.request(1)
}
[debug] application - onNext
[debug] application - onComplete!
Upvotes: 2