Postgresql Postgres JSONB:JSON数组中的查询值

Postgresql Postgres JSONB:JSON数组中的查询值,postgresql,psql,Postgresql,Psql,博士后9.4 我有一个JSONB值如下的记录: { "attributeA": 1, "attributeB": "Foo", "arrayAttribute": [ {"attributeC": 95, "attributeD": 5}, {"attributeC": 105, "attributeD": 5} ] } 我想写一个查询,它说: 查找attributeA=1,attributeB='Foo'的任何项目,对于arrayAttribute数组中的每个

博士后9.4

我有一个JSONB值如下的记录:

{
  "attributeA": 1,
  "attributeB": "Foo", 
  "arrayAttribute": [
   {"attributeC": 95, "attributeD": 5}, 
   {"attributeC": 105, "attributeD": 5}
  ]
}
我想写一个查询,它说:

查找attributeA=1,attributeB='Foo'的任何项目,对于arrayAttribute数组中的每个元素,attributeC在某个值X的10点范围内。因此,如果X为100,则上述记录将匹配(95和105都在100的10点范围内)

不幸的是,我真的很难使用JSONB查询语法。做这件事最好的方法是什么?

Postgres真的很棒。至于搜索查询方法,重要的是要知道
->
返回
文本
->
返回
json(b)

查询可以是以下内容:

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);
SELECT * FROM json js,jsonb_extract_path(data,'arrayAttribute') AS entireArray 
WHERE (entireArray -> 0 ->> 'attributeC')::integer = 95
AND (entireArray -> 1 ->> 'attributeC')::integer = 105;
博士后真的很棒。至于搜索查询方法,重要的是要知道
->
返回
文本
->
返回
json(b)

查询可以是以下内容:

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);
SELECT * FROM json js,jsonb_extract_path(data,'arrayAttribute') AS entireArray 
WHERE (entireArray -> 0 ->> 'attributeC')::integer = 95
AND (entireArray -> 1 ->> 'attributeC')::integer = 105;

谢谢,这很有帮助。但是,是否还有一种方法可以查询数组的特定索引?e、 g.如果arrayAttribute[0].attributeC=50和arrayAttribute[1].attributeC=100?谢谢,这很有帮助。但是,是否还有一种方法可以查询数组的特定索引?e、 g.如果arrayAttribute[0].attributeC=50和arrayAttribute[1].attributeC=100?