我怎样才能通过n1ql查询检索所有的孩子

时间:2019-06-01 02:23:34

标签: n1ql spring-data-couchbase

我打算在所有父对象中检索子元素,如何在数组对象的数组中检索它。

SELECT sd.* 
FROM test AS t
UNNEST stateDetails AS sd;
{
   "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"
            }
         ]
      }
   ]
}

我希望以下输出能够显示出所有国家/地区及其国家名称的详细信息

        [
            {
               "countryName":"US",
               "name":"Florida",
               "id":"1212"
            },
            {
               "countryName":"US",
               "name":"NewYork",
               "id":"1214"
            },
            {
               "countryName":"France",
               "name":"Grand Est",
               "id":"5212"
            },
            {
               "countryName":"France",
               "name":"Brittany",
               "id":"5214"
            }
         ]  

1 个答案:

答案 0 :(得分:0)

使用双重UNNEST并投影所需的内容

SELECT cd.name AS countryName, sd.name, sd.id
FROM test AS t
UNNEST t.countryDetails AS cd
UNNEST cd.stateInfo AS sd
WHERE t.type = "countries";