5

Given the following example document of collection:

{
  "timestamp": 1413543986,
  "message": "message",
  "readed": {
    "8": null,
    "9": null,
    "22": null
  },
  "type": "1014574149174"
}

How do I update the value of specific key in object with key "readed"? For example update value for key "8":

...
   "8": 10,
...
Community
  • 1
  • 1

1 Answers1

5

You can use MERGE or MERGE_RECURSIVE as follows:

db._query("FOR u IN test FILTER u._key == @key UPDATE u WITH
  'read': MERGE_RECURSIVE(u.read, { '8': 10 }) } IN test",
  { key: "11611344050" })

Merge will merge documents, where later values will overwrite earlier ones. See http://docs.arangodb.org/Aql/Functions.html for details.

fceller
  • 2,734
  • 15
  • 19
  • 1
    I think it would make sense to expose this function in the collection.update method via another parameter like so: collection.update(document, data, overwrite, keepNull, waitForSync, mergeRecursive) as updating an inner field is a common use case. – mikestaub Dec 21 '15 at 22:04
  • Typo: shouldn't it be `WITH { 'read': MERGE_RECURSIVE(u.read, { '8': 10 }) }`? (missing open `{`) – PJ_Finnegan May 16 '19 at 11:19