In a Postgresql (13+), a users table has a JSONB[] column (notifications) containing user notification. Notifications are populated by a simple dictionary, resulting in the following array:
[
{
"notification_id": "c20da296", # abbreviating UUID4
"level": "info",
"timestamp": ...,
"content": ...
},
.. more notifications ..
]
When a user dismisses a notification, i'd like to remove it from the array, using the notification's id (while leaving the other notifications). Many resources show how to deal with a JSONB nested structure (e.g., here) or across multiple users (e.g., here). Is there a way to achieve this, maybe using the - operator in some way? I'm expecting something along the following:
UPDATE users SET notifications - `{"notification_id": "c20da296"}`
WHERE user_id = "<user_uuid4>";
note: the user's identifier is known, there is no need to traverse all users to select for the relevant notification_id.