Upserting Document Fragments in 4.5 ( N1QL )


#1

In scanning the docs I cannot find how to update part of a document in N1QL in 4.5

for example - say the whole document looks like this: { “Active”: true, “Barcode”: “123456789”, “BrandID”: “9f3751ef-f14f-464a-bb86-854e99cf14c0”, “BuyCurrencyOverride”: “.37”, “BuyDiscountAmount”: “45.00”, “ID”: “003565a3-4a0d-47d9-befb-0ac642cb8057”, }

but I only want to work with part of the document as I don’t want to be selecting / updating the whole document in many cases:

{ “Active”: false, “Barcode”: “999999999”, “BrandID”: “9f3751ef-f14f-464a-bb86-854e99cf14c0”, “ID”: “003565a3-4a0d-47d9-befb-0ac642cb8057”, }

How can I use N1QL to just update those fields? Upsert completely replaces the whole document and update statement is not that clear. I can see this page http://developer.couchbase.com/documentation/server/4.5-dp/sub-doc-api.html shows how to do it , but not in N1QL.


#2

Hello,

The following N1QL statement lets you modify any part of a document.

UPDATE ...
SET ...
UNSET ...
WHERE ...
;

#3

Thanks , I’m not sure that you understand. I don’t want to update attributes etc individually - I want to send an object that updates only the parts of the document that are in the document fragment I send. http://developer.couchbase.com/documentation/server/4.5-dp/sub-doc-api.html It would be great to have an example in N1QL.


#4

Got it. We will add an OBJECT_CONCAT() function to concatenate two objects, which will do what you want. Until then, you can do the following in 4.5. Assuming your partial sub-object is in $1:

UPDATE mybucket d
SET d = OBJECT p.name : p.`value` FOR p IN ARRAY_CONCAT( OBJECT_PAIRS(d), OBJECT_PAIRS($1) ) END
WHERE ...;