我想通过存储对象数组的特定字段操作来检索数据。我想在其中添加新对象。
CREATE TABLE justjson ( id INTEGER, doc JSONB);
INSERT INTO justjson VALUES ( 1, '[
{
"name": "abc",
"age": "22"
},
{
"name": "def",
"age": "23"
}
]');
检索年龄大于等于 23 的数据怎么可能
我有这样的解决方案,但它会大大降低查询性能。
我的解决方案是使用 jsonb_array_elements:
t=# with a as (select *,jsonb_array_elements(doc) k from justjson)
select k from a where (k->>'age')::int >= 23;
k
------------------------------
{"age": "23", "name": "def"}
(1 row)
我需要一个解决方案或其他东西,通过它我可以高性能地做这样的事情。