Dimitar
Dimitar

Reputation: 21

N1QL: add new object to existed couchbase document

Is it possible using N1QL?

For example i have this:

{
"blog": "Coffee",
"user_id": 41,
"comments": [
    {
        "comment": "cup",
        "user_id": 883
    },
    {
        "comment": "water",
        "user_id": 790
    }
  ]
}

And I want using N1QL add sugar to comments, to result this: { "blog": "Coffee", "user_id": 41, "comments": [ { "comment": "cup", "user_id": 883 }, { "comment": "water", "user_id": 790 }, { "comment": "sugar", "user_id": 14 } ] }

I tried this:

UPDATE
    Blog
SET
    `c.comment` = "sugar",
    `c.user_id` = 14
FOR
    c IN comments
WHERE 
    `blog` = "Coffee" 
// [{"code":3000,"msg":"syntax error - at WHERE"}

And this:

UPDATE 
    Blog
SET
    ("comments", { "comment": "sugar", "user_id": 14})
WHERE
    `blog` = "Coffee"
//[{"code":3000,"msg":"syntax error - at ("}

Upvotes: 2

Views: 1358

Answers (1)

geraldss
geraldss

Reputation: 2445

Yes, you can perform any modification using N1QL.

UPDATE Blog
SET comments = ARRAY_APPEND( comments, { "comment":"sugar", "user_id":14 } )
WHERE blog = "Coffee";

Upvotes: 5

Related Questions