多次更新插入ArangoDB

qij5mzcb  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(106)

我的收藏:

{"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不起作用。

q5lcpyga

q5lcpyga1#

我想你的意思是:

UPSERT { "date": "5-1-2020", "country": "US" }
INSERT { "date": "5-1-2020", "country": "US", "score": 17 }
UPDATE { "score": 17 } IN collection

根据日期和国家/地区查找文档并更新其分数,或插入包含日期、国家/地区和分数的新文档。请注意,没有外部FOR循环,该循环将不必要地循环访问集合,并可能导致相同的更新执行数百次。

相关问题