如何在 PostgreSQL 中使用 JSONB 更新列内的属性并将数据保留在列内

How in PostgreSQL to update an attribute inside a column with a JSONB and also keeping the data already inside

我有一种情况,在一个名为 tokens 的 table 中,我有一个名为 data 的列 数据列由类似 '{}'::jsonb

的内容组成
{"recipientId": "xxxxxxxx"}

我的目标是按照新的数据库设计和要求更新旧数据

{"recipientIds": ["xxxxxxxx"]}

原因是命名已更改,值将是一个收件人数组。

我不知道如何实现这个

table 如下所示是一个简单的 table,其中包含几列。 数据是唯一的'{}'::jsonb.

id 类型 数据
1 type1 数据1
2 type2 数据1

作为编辑,我尝试做并部分解决了我的问题,但无法理解如何将值设置为 [value]

update
    "token"
set
    "data" = data - 'recipientId' || jsonb_build_object('recipientIds', data->'recipientId')
where
    "type" in ('INVITE_ECONSENT_RECIPIENT')

我现在可以 recipientids: value 但需要 recipientids: [value]

你很接近,你需要传递一个数组作为 jsonb_build_object() 函数的第二个参数:

(data - 'recipientId')||jsonb_build_object(
                            'recipientIds', 
                            jsonb_build_array(data -> 'recipientId')
                        )