In the document above, there is no “location” object. During my query, Id like to check and see if the object exists before I try to add values into it.
I tried using the CASE statement, but it doesn’t seem to work this way as everything after the SET clause expects key/value representations. I checked the Object functions, but they all require an input object, and there seems to be no way to provide the document being updated into these functions.
So basically the document would look like this after:
{
"items":{...},
"location": {...}
}
Given the fluidity of JSON structures, it would be cool to be able to perform this kind of function during an update.
So, do I have any options? Or do I need to do a select/update, first?
UPDATE default USE KEYS "k100"
SET `location` = CASE WHEN `location` IS MISSING THEN {"hello":"world"}
ELSE OBJECT_ADD(`location`, "hello", "world") END
So, lets say any part of a sub-document does not exist yet, but I am trying to write a new value to it. Id like to create that object/array during query execution.
My solution above works, but its not possible to use it in a loop, it seems. So it becomes impossible to do it for nested documents. At least to my knowledge.
Assuming “key_1” and “key_2” are supposed to have identical structures, Id like to create “inner_inner_key_2” in one update query, first checking to see if it exists. If it doesn’t, create it, if it does, update it.
I’ve been playing around with different mixes of CASE and SET, but can’t seem to get the engine to do it. It may not be possible, but I want to make sure I ask it here, first.