【发布时间】:2020-07-28 05:17:43
【问题描述】:
我的收藏:
{"date": "5-1-2020", "country": "US", score: 0}
{"date": "5-1-2020", "country": "CA", score: 10}
{"date": "5-2-2020", "country": "US", score: 7}
{"date": "5-2-2020", "country": "CA", score: 11}
这是场景:我需要将 2020 年 5 月 1 日美国比赛的比分更新为 17。
伪代码:
Search for date: "5-1-2020" and country "US".
If found UPDATE score to 17.
If not found insert record of {"date": "5-1-2020", "country": "US", score: 17}
我的工作解决方案:
FOR doc in collection
UPSERT { "date": "5-1-2020", "country": "US" }
INSERT doc
UPDATE { "score": 17 } in collection
如果我的收藏是:
{"date": "5-1-2020", "country": "CA", score: 10}
{"date": "5-2-2020", "country": "US", score: 7}
{"date": "5-2-2020", "country": "CA", score: 11}
解决方案/upsert 不起作用。
【问题讨论】:
-
您真的想在更新某些文档时将所有文档从
collection复制到newcol吗?或者您想从collection中取出一个匹配的文档并将其插入newcol,可能带有更新的分数? -
抱歉,更新了代码。集合名称应该是相同的。