如何在 n1ql 查询中检索父项的子项
how to retrieve the child of the parent in n1ql query
我打算根据某些条件检索父元素的子元素。我如何检索它,在数组对象数组中。
SELECT ARRAY {s.name,s.id} FOR s IN t.countryDetails.stateInfo END AS stateDetails
FROM test AS t
WHERE t.type = "countries" and t.countryDetails.name = 'US';
这是我要查询的实际 json 文档:
{
"type":"countries",
"docName":"CountryData",
"countryDetails":[
{
"name":"US",
"code":"+1",
"stateInfo":[
{
"name":"Florida",
"id":"1212"
},
{
"name":"NewYork",
"id":"1214"
}
]
},
{
"name":"France",
"code":"+33",
"stateInfo":[
{
"name":"Grand Est",
"id":"5212"
},
{
"name":"Brittany",
"id":"5214"
}
]
}
]
}
我期望以下输出仅显示美国的国家/地区详细信息:
[
{
"name":"Florida",
"id":"1212"
},
{
"name":"NewYork",
"id":"1214"
}
]
如果您的 ConutryDetails 每个国家只有一个条目,请使用以下内容
SELECT FIRST s.stateInfo FOR IN t.countryDetails WHEN s.name = "US" END AS stateDetails
FROM test AS t
WHERE t.type = "countries"
AND ANY v IN t.countryDetails SATISFIES v.name = 'US' END;
或
SELECT cd.stateInfo AS stateDetails
FROM test AS t
UNNEST t.countryDetails AS cd
WHERE t.type = "countries" AND cd.name = 'US';
我打算根据某些条件检索父元素的子元素。我如何检索它,在数组对象数组中。
SELECT ARRAY {s.name,s.id} FOR s IN t.countryDetails.stateInfo END AS stateDetails
FROM test AS t
WHERE t.type = "countries" and t.countryDetails.name = 'US';
这是我要查询的实际 json 文档:
{
"type":"countries",
"docName":"CountryData",
"countryDetails":[
{
"name":"US",
"code":"+1",
"stateInfo":[
{
"name":"Florida",
"id":"1212"
},
{
"name":"NewYork",
"id":"1214"
}
]
},
{
"name":"France",
"code":"+33",
"stateInfo":[
{
"name":"Grand Est",
"id":"5212"
},
{
"name":"Brittany",
"id":"5214"
}
]
}
]
}
我期望以下输出仅显示美国的国家/地区详细信息:
[
{
"name":"Florida",
"id":"1212"
},
{
"name":"NewYork",
"id":"1214"
}
]
如果您的 ConutryDetails 每个国家只有一个条目,请使用以下内容
SELECT FIRST s.stateInfo FOR IN t.countryDetails WHEN s.name = "US" END AS stateDetails
FROM test AS t
WHERE t.type = "countries"
AND ANY v IN t.countryDetails SATISFIES v.name = 'US' END;
或
SELECT cd.stateInfo AS stateDetails
FROM test AS t
UNNEST t.countryDetails AS cd
WHERE t.type = "countries" AND cd.name = 'US';