首页 > 解决方案 > 对 CouchBase 编程的热情

问题描述

我想获取对象数组中的 Couchbase 文档中的单个元素,但我能够获取对象数组

我尝试使用以下查询获取数组,'select countryDetails from test';

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

我尝试使用获取数组,select countryDetails from test;

我喜欢获取结果[ {"name" : "US", "code" : "+1" }, {"name" : "France", "code" : "+33"}]

标签: couchbasen1qlspring-data-couchbase

解决方案


如果您投影 countryDetails 它会投影整个子对象。如果你需要子对象的一部分,你需要明确地投影它。

以下 ARRAY 构造将提供您期望的数据表示。

SELECT ARRAY {v.name,v.code} FOR v IN t.countryDetails END AS contryDetails
FROM test AS t 
WHERE t.type = "countries";

推荐阅读