【问题标题】:how to retrieve the child of the parent in n1ql query如何在n1ql查询中检索父母的孩子
【发布时间】:2019-05-31 19:10:47
【问题描述】:

我计划根据某些条件检索父级的子元素。我如何检索它,在数组对象的数组中。

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"
            }
         ] 

【问题讨论】:

    标签: couchbase n1ql spring-data-couchbase


    【解决方案1】:

    如果您的 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';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      相关资源
      最近更新 更多