Update on deeply nested Documents


#1

It was shown that subdocuments can be updatet in Update on nested Documents.
Is it also possible to update deeply nested documents?

For example updating all replies from the User “user_id”: “342” with the attribute “category” : “top user” in the following document:

{
  "blog": "testblog1",
  "user_id": 41,
  "comments": [
    {
      "comment": "testcomment1",
      "user_id": 883,
      "replies": [
        {
          "text": "reply on testcomment1",
          "user_id": 342
        },
        {
          "text": "another reply",
          "user_id": 473
        }
      ]
    },
    {
      "comment": "testcomment2",
      "user_id": 790
    }
  ]
}

Note: When knowing on which comment the user replied I could do the following:

UPDATE Blog SET r.category= ‘top user’ FOR r IN comments[0].replies WHEN r.user_id = 342 END;

What I am looking for is a approach to achieve an update on all comments, like comments[*].replies

The N1QL Language Reference is often very helpful, but in this case does not provide any information about this.


#2

Hello,

You can use WITHIN. For example:

UPDATE Blog b SET r.category= ‘top user’ FOR r WITHIN b.comments WHEN r.user_id = 342 END;

This will update both comments and replies, regardless of nesting depth.

Gerald


#3

Thank you for the fast and helpful response, this is exactly what I was looking for. Awesome!