How to update a value inside a array of objects

Given docs like:

{
  "test": [
    {value: 1, other_value: 2, other_value_2:  "test"},
    {value: 2, other_value: 1,  other_value_2: "test"},
    {value: 2, other_value: 2,  other_value_2: "test"}
  ]
},
{
  "test": [
    {value: 1, other_value: 2,  other_value_2: "test"},
    {value: 2, other_value: 1,  other_value_2: "test"},
    {value: 2, other_value: 2,  other_value_2: "test"}
  ]
}

Who would I go about updating other_value_2 to e.g. “test2” in all documents where value is 1 and other_value is 2?

UPDATE default  AS d
SET  v.other_value2 = "test2"  FOR v IN d.test WHEN v.`value` = 1 AND v.other_value = 2 END
WHERE ANY v1 IN d.test SATISFIES v1.`value` = 1 AND v1.other_value = 2  AND v1.other_value2 != "test2" END;

Check last example https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/update.html

1 Like