[英]Multi Upsert ArangoDB
My collection:我的收藏:
{"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}
Here is the scenario: I need to update the score for the US game on 5-1-2020 to 17.这是场景:我需要将 2020 年 5 月 1 日美国比赛的比分更新为 17。
Pseudocode:伪代码:
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}
My working solution:我的工作解决方案:
FOR doc in collection
UPSERT { "date": "5-1-2020", "country": "US" }
INSERT doc
UPDATE { "score": 17 } in collection
If my collection was:如果我的收藏是:
{"date": "5-1-2020", "country": "CA", score: 10}
{"date": "5-2-2020", "country": "US", score: 7}
{"date": "5-2-2020", "country": "CA", score: 11}
The solution/upsert doesn't work.解决方案/upsert 不起作用。
I think what you mean to do is this:我想你的意思是这样的:
UPSERT { "date": "5-1-2020", "country": "US" }
INSERT { "date": "5-1-2020", "country": "US", "score": 17 }
UPDATE { "score": 17 } IN collection
Find document based on date and country and update its score, or insert a new document with date, country and score.根据日期和国家/地区查找文档并更新其分数,或插入带有日期、国家和分数的新文档。 Note that there is no outer FOR loop, which would iterate over the collection unnecessarily and potentially cause the same update to be performed hundred of times.
请注意,没有外部 FOR 循环,它会不必要地遍历集合,并可能导致执行相同的更新数百次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.