sum*_*ght 0 scala reactivemongo
当我找到时,我从MongoDB返回以下文档列表"campaignID":"DEMO-1".
[
{
"_id": {
"$oid": "56be0e8b3cf8a2d4f87ddb97"
},
"campaignID": "DEMO-1",
"revision": 1,
"action": [
"kick",
"punch"
],
"transactionID": 20160212095539543
},
{
"_id": {
"$oid": "56c178215886447ea261710f"
},
"transactionID": 20160215000257159,
"campaignID": "DEMO-1",
"revision": 2,
"action": [
"kick"
],
"transactionID": 20160212095539578
}
]
Run Code Online (Sandbox Code Playgroud)
现在,我在这里尝试做的是给定的campaignID我需要找到它的所有版本(在我的情况下是修订版)并将action字段修改为deadString类型.我阅读了文档,他们的例子太简单了,在我的案例中没有太大帮助.这就是文档所说的:
val selector = BSONDocument("name" -> "Jack")
val modifier = BSONDocument(
"$set" -> BSONDocument(
"lastName" -> "London",
"firstName" -> "Jack"),
"$unset" -> BSONDocument(
"name" -> 1))
// get a future update
val futureUpdate = collection.update(selector, modifier)
Run Code Online (Sandbox Code Playgroud)
我不能只关注文档,因为它很容易创建一个新的BSON文档,并通过硬编码确切的字段使用它来修改BSON结构.在我的情况下,我需要先找到文档,然后动态修改action字段,因为与文档不同,我的action字段可以有不同的值.
到目前为止,我的代码显然无法编译:
def updateDocument(campaignID: String) ={
val timeout = scala.concurrent.duration.Duration(5, "seconds")
val collection = db.collection[BSONCollection](collectionName)
val selector = BSONDocument("action" -> "dead")
val modifier = collection.find(BSONDocument("campaignID" -> campaignID)).cursor[BSONDocument]().collect[List]()
val updatedResults = Await.result(modifier, timeout)
val mod = BSONDocument(
"$set" -> updatedResults(0),
"$unset" -> BSONDocument(
"action" -> **<???>** ))
val futureUpdate = collection.update(selector, updatedResults(0))
futureUpdate
}
Run Code Online (Sandbox Code Playgroud)
这对我有用,可以回答我自己的问题.谢谢@cchantep帮助我.
val collection = db.collection[BSONCollection](collectionName)
val selector = BSONDocument("campaignID" -> campaignID)
val mod = BSONDocument("$set" -> BSONDocument("action" -> "dead"))
val futureUpdate = collection.update(selector, mod, multi = true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1760 次 |
| 最近记录: |