How to update document by adding key-value in Couchbase




Suppose I have a document

{“key1”:value1, “key2”:“value2”}

How can I use N1QL to update the document to have another key value pair?

{“key1”:value1, “key2”:“value2”,“key3”:value3}

I have looked at the update set features, but it’s to update key values that are already present.



Simply set the new key-value.

UPDATE bucket SET key3 = "value3" WHERE "key1" = "value1";

UPDATE works in sets like SQL. If you know the document to update, use the USE KEYS clause otherwise, use the WHERE clause carefully.

UPDATE bucket USE KEYS "mykey" SET key3 = "value3" ;