如何在n1ql查询中检索父级的子级

时间:2019-05-31 19:10:48

标签: couchbase n1ql spring-data-couchbase

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

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

1 个答案:

答案 0 :(得分:0)

如果您的ConutryDetails在每个国家/地区只有一个条目,请使用以下

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div id="page">
  <div>
    <button id="run">Run</button>
    <button id="reset">Reset</button>
  </div>
  <svg id="container">
    <g x="0" y="0" id="group" width="100%" height="100%">
      <line x1="20" y1="50" x2="150" y2="150" stroke="brown" />
      <svg x="20" y ="50">
        <circle cx="0" cy="0" r="35" stroke="red" fill="blue">
        <text x="35" y="0" height="100%" width="100%">a bunch of text</text>
      </svg>
      <line x1="100" y1="350" x2="160" y2="340" stroke="brown" />
      <svg x="100" y ="350">
        <circle cx="0" cy="0" r="35" stroke="red" fill="blue">
        <text x="35" y="0" height="100%" width="100%">a bunch of text 3</text>
      </svg>
    </g>
  <svg>
<div>

OR

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;