【问题标题】:Multi Upsert ArangoDBMulti Upsert ArangoDB
【发布时间】: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,可能带有更新的分数?
  • 抱歉,更新了代码。集合名称应该是相同的。

标签: node.js arangodb


【解决方案1】:

我想你的意思是这样的:

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

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

【讨论】:

    猜你喜欢
    • 2021-08-24
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    相关资源
    最近更新 更多