postgresql 更新以从 JSONB 数据类型中删除 key/value
postgresql UPDATE to remove key/value from JSONB data type
给定以下 table 和数据:
CREATE TABLE test (
slots jsonb
);
INSERT INTO test VALUES ('{"0": {"tag": "abc", "info": "xyz"}, "1": {"tag": "def", "info": "uvw"}}');
SELECT slots FROM test;
现在我想完全删除 "1"
key/value。我可以将它设置为 null
,如下所示:
UPDATE test SET slots['1'] = null;
SELECT slots FROM test;
但是这个 returns:
{"0": {"tag": "abc", "info": "xyz"}, "1": null}
而我希望它 return:
{"0": {"tag": "abc", "info": "xyz"}}
实现这个的command/syntax是什么?
使用 -
运算符完全删除密钥
update test
set slots = slots - '1'
给定以下 table 和数据:
CREATE TABLE test (
slots jsonb
);
INSERT INTO test VALUES ('{"0": {"tag": "abc", "info": "xyz"}, "1": {"tag": "def", "info": "uvw"}}');
SELECT slots FROM test;
现在我想完全删除 "1"
key/value。我可以将它设置为 null
,如下所示:
UPDATE test SET slots['1'] = null;
SELECT slots FROM test;
但是这个 returns:
{"0": {"tag": "abc", "info": "xyz"}, "1": null}
而我希望它 return:
{"0": {"tag": "abc", "info": "xyz"}}
实现这个的command/syntax是什么?
使用 -
运算符完全删除密钥
update test
set slots = slots - '1'