Hi,
I am trying to individual update on fields ‘to’ and ‘from’
I am trying with this query
UPDATE doc
SET userss.to= null
FOR userss in UserGroup.user
FOR UserGroup in UserGroups
WHEN UserGroup.name == “name1” and userss.id="user1"
END,
userss.from= null
FOR userss in UserGroup.users
FOR UserGroup in UserGroups
WHEN UserGroup.name == “name1” and userss.id="devxyz1"
END
but it is taking more time and sometime facing issue on update too
Is there any other way to update ?
Can anyone help me on this??
If you need to update array element with condition you need to use FOR …IN …WHEN. If You know exact position of array or whole array you can update without FOR.
Actually the concern is can we write the query like:
UPDATE default SET userss.to = null FOR userss , userss.from = null
FOR userss IN UserGroup.user FOR UserGroup IN UserGroups WHEN UserGroup.name = “name1” AND userss.id=“user2” END;
So that one we have smaller query, and it seems like we are going in the document multiple times if we are updating multiple properties in a subdcument.
If UserGroup.user objects are unique you can reconstruct user object and replace it. like below.
UPDATE default
SET UserGroup.`user`[ARRAY_POS(UserGroup.`user`,userss)] = {"to" : null, "from":null, "id":userss.id}
FOR userss IN UserGroup.`user` FOR UserGroup IN UserGroups WHEN UserGroup.name = "name1" AND userss.id="user1" END;
For not unique
UPDATE default
SET UserGroup.`user` = ARRAY CASE WHEN userss.id="user1" THEN {"to" : null, "from":null, "id":userss.id} ELSE userss END FOR userss IN UserGroup.`user` END
FOR UserGroup IN UserGroups WHEN UserGroup.name = "name1" END;
Thanks for the update, i think we are looking for reconstruct the object since there could be more properties to what we are updating, if we reconstruct we would loose that data.
I think in that case we are left with the multi update as we were doing earlier.