postgres 中有(v 9.5,如果有的话):
create table json_test(
id varchar NOT NULL,
data jsonb NOT NULL,
PRIMARY KEY(id)
);
其中 data 是 json 并且包含数组的数组
{
"attribute": "0",
"array1": [{
"id": "a12",
"attribute": "1",
"array2": [{
"id": "a21",
"attribute": "21"
}]
},
{
"id": "a12",
"attribute": "2",
"array2": [{
"id": "22",
"attribute": "22"
}]
}]
}
必需的:
select id from json_test where
json_test->>'attribute'='0' and
array1.[id='a12'].array2.attribute='22'
查询的意思应该是:给我所有的 id
- 一些顶级属性具有特定值
- 数组中的特定对象具有必需的属性
- 某些对象(来自 array2)特别是 array1 具有必需的属性
诀窍是如何实现最后一个条件。
另一个例子:
{
"attribute": "0",
"array1": [{
"id": "a12",
"attribute": "1",
"array2": [{
"id": "a21_1",
"attribute_1": "21_1"
},{
"id": "a21_2",
"attribute_2": "21_2"
}]
}]
}
select * from json_test where
array1.[id='a12'].array2.attribute_1='21_1' and
array1.[id='a12'].array2.attribute_2='21_2'