Database Reference
In-Depth Information
Updating discrete values in a map
When we allow a user to link up to a new social network, we'd like to be able to add their
ID to the social_identities collection without regard to its current contents. To do
so, we use a syntax similar to that of element replacement in a list:
UPDATE "users"
SET "social_identities"['instagram'] = 9839025,
"social_identities"['yo'] = 25
WHERE "username" = 'alice';
It's also perfectly valid to overwrite a value at an existing key; since keys are unique, the
previous value will be replaced with the given one:
UPDATE "users"
SET "social_identities"['twitter'] = 2725634
WHERE "username" = 'alice';
Checking the value of the map in alice 's row, we can confirm that the Twitter ID is the
most recently written one, along with the Instagram and Yo values:
Note that, though the syntax for a map update looks like list replacement, updating a key-
value pair in a map does not carry the performance penalty that replacing a list item does.
Removing values from maps
Continuing the parallels, we can remove a value from a map using the DELETE statement,
in a similar way to removing items from a list by index:
DELETE "social_identities"['instagram']
FROM "users"
WHERE "username" = 'alice';
Search WWH ::




Custom Search