Append Json data to existing document

Hi,

I am very new to couchbase and N1QL, I want to append the json data to existing document.
I found (UPDATE bucketname d USE KEYS “arr_key” SET d = ARRAY_APPEND(d, “baz”) WHERE “baz” NOT IN d) but its not for json data.

Can someone help.

N1QL query works on JSON data. What’s the issue/error you have?

select meta().id, * from sales;
  {
    "id": "k1",
    "sales": {
      "a": 99,
      "b": 2,
      "c": 99,
      "d": {
        "a": 88
      },
      "e": [
        1,
        2,
        3,
        4
      ]
    }
  }

update sales set e = array_append(e, 99) where 99 not in e;

select meta().id, * from sales;
[
  {
    "id": "k1",
    "sales": {
      "a": 99,
      "b": 2,
      "c": 99,
      "d": {
        "a": 88
      },
      "e": [
        1,
        2,
        3,
        4,
        99
      ]
    }
  }
]
1 Like

Thanks Keshav,

In your example you added 99 but I want to append key value pairs.

How’s that can be done.

Regards,
Vinayak

Please give BEFORE and AFTER update content of the JSON document

Let’s say, we want to add “d”: 55, “z”: 33 in your example.

Regards,
Vinayak

Do u want to add this to array or field in object. Please provide sample document before and after.

UPDATE sales set d1 = 55, z = 33 where 99 not in e;

{
Data:
[
{
“Name”: "Name1"
“Location”: “UK”
“Date”: “10-11-2010”
}
{
“Name”: "Name2"
“Location”: “UK”
“Date”: “21-12-2012”
}
]
}

Now I want to append few more set of name, Location and Date in Data. The
bucket name is ‘Record’ and Document name (key) is ‘Lastknown’

Regards,
Vinayak

UPDATE `Record` USE KEYS "Lastknown" 
SET `Data` = ARRAY_APPEND(`Data`, {"Name": "name3", "Location": "USA", "Date": "21-12-2017" });

Date is stored in ISO-8601 format date functions described here can be used https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/datefun.html

1 Like

It Worked, Thank you so much guys.