N1ql object_put on a missing object

Hi,

I am trying to update a document object using object_put but for some reason if the object is missing completely it is not added. I have also tried to add it before the set if is missing, but still object_put doesn’t seems to find it

UPDATE `default`USE KEYS "a" 
SET `contextual_tags_data` = IFMISSING(`contextual_tags_data`, {}), `contextual_tags_data` =  OBJECT_PUT(`contextual_tags_data`, "1-1-2", {"tag_source": "auto"}), 
updated_timestamp = TRUNC(NOW_MILLIS()/1000 ,0) returning *

Is there a way in which I can do this?

As a single statement you could try:

UPDATE default SET `contextual_tags_data` = OBJECT_PUT(IFMISSING(`contextual_tags_data`,{}),"1-1-2",{"tag_source":"auto"});

(ref: Conditional Functions for Unknowns | Couchbase Docs)
or

UPDATE default SET `contextual_tags_data` = OBJECT_PUT(CASE WHEN `contextual_tags_data` IS MISSING THEN {} ELSE `contextual_tags_data` END,"1-1-2",{"tag_source":"auto"});

or

UPDATE default SET `contextual_tags_data` = CASE WHEN `contextual_tags_data` IS MISSING THEN {"1-1-2":{"tag_source": "auto"}} ELSE OBJECT_PUT(`contextual_tags_data`,"1-1-2",{"tag_source":"auto"}) END ;

or as 2 statements, one to update to {} where the field is missing, and the second a simple OBJECT_PUT.

HTH.

1 Like

thanks so much the first one works perfect