简体   繁体   English

我无法在ArangoDB的FOR循环中多次更新同一文档

[英]I can not update same document multiple times in FOR loop in ArangoDB

I have a collection or users and I need to update the property "cuantity" twice in a loop. 我有一个集合或用户,我需要循环更新两次属性“ cuantity”。 For example in the update array I need to to increase it by 1 and then increase it by 2, so if the actual value is 1 the result value must be 4,but it does not happen this way. 例如,在更新数组中,我需要将其增加1,然后将其增加2,因此,如果实际值为1,则结果值必须为4,但这种方式不会发生。 ArangoDB seams to take the last update object in the update array loop and dismiss the previous update objects in the loop. ArangoDB接缝以获取update数组循环中的最后一个更新对象并关闭循环中的先前更新对象。

This is my collection (for make testing simple): 这是我的收藏(用于简化测试):

{"_key": "1", "name":"ivan", "cuantity":1}
{"_key": "2", "name":"juan", "cuantity":1}
{"_key": "3", "name":"carl", "cuantity":1}

This is my query and the update array 这是我的查询和更新数组

let updateData = [
  { user_key: "1", cuantity: 1},
  { user_key: "1", cuantity: 2},
  { user_key: "2", cuantity: 1},
  { user_key: "2", cuantity: 3}
  ]

FOR e IN updateData
  LET doc = DOCUMENT(CONCAT("test1/",e.user_key))
  UPDATE doc WITH { cuantity: doc.cuantity + e.cuantity }
  IN test1
  LET updated = NEW
RETURN updated

This is the result 这是结果

[
  {
    "_key": "1", "_id": "test1/1",
    "name": "ivan",
    "cuantity": 2
  },
  {
    "_key": "1", "_id": "test1/1",
    "name": "ivan",
    "cuantity": 3
  },
  {
    "_key": "2", "_id": "test1/2",
    "name": "sergio",
    "cuantity": 2
  },
  {
    "_key": "2", "_id": "test1/2",
    "name": "sergio",
    "cuantity": 4
  }
]

The result of that operation must be that user with "_key": "1" has "cuantity": 4, because in the first iteration it was increased by 1 and in the second iteration by 2. The problem seems to be that ArangoDB cant update the document based in a previous update in the same loop. 该操作的结果必须是“ _key”:“ 1”的用户具有“ cuantity”:4,因为在第一次迭代中它增加了1,在第二次迭代中增加了2。问题似乎是ArangoDB无法在同一循环中基于先前的更新来更新文档。 ¿There is a workaroud oa configuration to achieve this? 有一个可实现此目的的工作配置? I am using RocksDB engine. 我正在使用RocksDB引擎。

Arango 3.4 seems to always update the original value when doing updates in a loop. Arango 3.4似乎总是在循环执行更新时更新原始值。 I just ran this on my machine directly in the webGUI : 我只是直接在webGUI中在我的机器上运行了它:

let myData = [
{ user_key: "1", cuantity: 1},
{ user_key: "1", cuantity: 2},
{ user_key: "2", cuantity: 1},
{ user_key: "2", cuantity: 3},
{ user_key: "3", cuantity: 2}
]

For entry in myData
  LET doc = DOCUMENT(CONCAT("test4/",entry.user_key))
  UPDATE doc WITH { cuantity: doc.cuantity + entry.cuantity }
  IN test4
  LET updated = NEW
RETURN updated

Assuming cuantity for user_key was 1 before the update, I ended up with the incorrect value of 3 instead of 4 by the end of the update. 假设在更新之前user_key的要求为1,我最终在更新结束时得到的错误值为3而不是4。

Having said that, I would think that you may want to aggregate your data before doing the update. 话虽如此,我认为您可能希望在进行更新之前汇总数据。 For example, if the value for user_key = 1 was updated 100 times in the batch, you probably do not want to run the update 100 times as it could lead to a performance issue. 例如,如果在批处理中将user_key = 1的值更新了100次,则您可能不想运行更新100次,因为这可能会导致性能问题。 If you pre-aggregate, you could just update the record once at the end. 如果进行预汇总,则只需在末尾更新一次记录即可。 So for example you could do something similar to the following : 因此,例如,您可以执行以下操作:

let myData = [
{ user_key: "1", cuantity: 1},
{ user_key: "1", cuantity: 2},
{ user_key: "2", cuantity: 1},
{ user_key: "2", cuantity: 3},
{ user_key: "3", cuantity: 2}
]


 LET aggData = ( FOR entry IN myData
  COLLECT theKey = entry.user_key 
  AGGREGATE addedCuantity = sum(entry.cuantity)
  RETURN {theKey, addedCuantity}
  )

  FOR aggEntry IN aggData
  LET doc = DOCUMENT(CONCAT("test4/",aggEntry.theKey))
  UPDATE doc WITH { cuantity: doc.cuantity + aggEntry.addedCuantity }
  IN test4
  LET updated = NEW
  RETURN updated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM