Couchbase查询数组

时间:2017-01-05 09:00:22

标签: json couchbase n1ql

我将以下json保存在我的couchbase存储桶"geo"中。

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "ID": "1753242",
        "TYPE": "8003"
      }
    },
    {
      "type": "Feature",
      "properties": {
        "ID": "4823034",
        "TYPE": "7005"
      }
    },
    {
      "type": "Feature",
      "properties": {
        "ID": "4823034",
        "TYPE": "8003"
      }
    }
  ]
}

要使用"features"获取所有"properties.TYPE : 8003",我尝试了以下操作。

SELECT features FROM geo
WHERE ANY f IN features SATISFIES f.properties.TYPE = "8003" END;

但这会返回整个json文档,而不只是"features"properties.TYPE "8003"。 有没有人知道,如何查询以获得"properties.TYPE": "8003"的匹配功能?

1 个答案:

答案 0 :(得分:2)

WHERE子句中的ANY表达式仅用于过滤感兴趣的文档。如果需要特定的项目列表,还需要在投影列表中编写相应的表达式。您的查询中的投影要求提供'功能'因此整个'功能'正在返回数组。您可以在投影列表上编写以下表达式以获得所需的输出:

SELECT ARRAY f FOR f IN features WHEN f.properties.TYPE = "8003" END
FROM geo
WHERE ANY f IN features SATISFIES f.properties.TYPE = "8003" END;
[
  {
    "$1": [
      {
        "properties": {
          "ID": "1753242",
          "TYPE": "8003"
        },
        "type": "Feature"
      },
      {
        "properties": {
          "ID": "4823034",
          "TYPE": "8003"
        },
        "type": "Feature"
      }
    ]
  }
]

HTH,

-Prasad