更新时间:2023-02-05 14:46:17
Postgres 关于json的文档确实很棒。对于搜索查询方法,重要的是要知道->>
返回 text
和 ->
返回 json(b)
。
Postgres documentation regarding json is really great. As for search query approach it's important to know that ->>
returns text
and ->
returns json(b)
.
查询可以是以下内容:
Query can be the following:
select * from json js,jsonb_array_elements(data->'arrayAttribute') as array_element
where (js.data->>'attributeA')::integer = 1
and js.data->>'attributeB' = 'Foo'
and (array_element->>'attributeC')::integer >= (100-5)
and (array_element->>'attributeC')::integer <= (100+5);
如果要按索引选择特定的数组元素,则查询如下:
If you want to select particular array element by index, in your case query will be the following:
SELECT * FROM json js,jsonb_extract_path(data,'arrayAttribute') AS entireArray
WHERE (entireArray -> 0 ->> 'attributeC')::integer = 95
AND (entireArray -> 1 ->> 'attributeC')::integer = 105;