简体   繁体   中英

Update an array value in nested document in ArangoDB

`Given the following document inside a collection card: I have to update the whole data value for a particular id in staticCard



`

{
  "staticCards": [
    {
         id:123, 
         search:"",
         data:[]

    },
    {
         id:456,
         search:"",
         data:[]
    },
],
    "dynamicCards":[
      {
         id:789,
         search:"",
         data:[]
      },
      {
         id:127,
         search:"",
         data:[]
      },
      {}
    ]
}

You need to determine the index of the array element, which isn't straightforward if you want to match one of the object attributes instead of the whole object with POSITION(). Then you can use REPLACE_NTH() to set a new value. Finally, you need to update the respective top-level attribute.

LET pos = FIRST(FOR i IN 0..LENGTH(doc.dynamicCards)-1
  FILTER doc.dynamicCards[i].id == 127
  LIMIT 1
  RETURN i
)
LET new = REPLACE_NTH(doc.dynamicCards, pos, { id: 128, search: "", data: [] })
UPDATE doc WITH { dynamicCards: new } IN coll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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