用于从对象数组中选择值的Couchbase N1QL查询

用于从对象数组中选择值的Couchbase N1QL查询,couchbase,n1ql,Couchbase,N1ql,从对象数组中选择值的正确方法是什么 要检索注释的不同列表 [ "comment 1", "comment 2", "comment 3", "comment 4" ] 当前N1QL查询 SELECT links[*].comment FROM `my-db` WHERE type = "links" AND ANY l IN links SATISFIES l.comment I

从对象数组中选择值的正确方法是什么

要检索注释的不同列表

[
  "comment 1",
  "comment 2",
  "comment 3",
  "comment 4"
]
当前N1QL查询

SELECT links[*].comment FROM `my-db`
WHERE type = "links" AND ANY l IN links SATISFIES l.comment IS NOT MISSING END
结果

[
  {
    "comment": [
      "comment 1"
    ]
  },
  {
    "comment": [
      "comment 1",
      "comment 2"
    ]
  },
  {
    "comment": [
      "comment 3",
      "comment 4"
    ]
  }
]
给定一系列包含注释字段的文档:

{
  "type": "links",
  "links": [
    {
      "comment": "comment 1"
    }
  ]
}

{
  "type": "links",
  "links": [
    {
      "comment": "comment 1"
    },
    {
      "comment": "comment 2"
    }
  ]
}

{
  "type": "links",
  "links": [
    {
      "comment": "comment 3"
    },
    {
      "comment": "comment 4"
    }
  ]
}
使用最不合适的

SELECT DISTINCT RAW l.comment 
FROM `my-db` AS m
UNNEST m.links AS l
WHERE m.type = "links" AND l.comment IS NOT NULL;


以上两种方法都返回了预期的结果。第二个确实有一个数组的双重嵌套(例如:['comment1','comment2']]),但这在解析中很容易解决。应用建议的索引后,两者的响应时间均为15毫秒。请将ARRAY_flatte()第二个参数更改为2,然后重试
If data set is not too large.

SELECT RAW  ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY_AGG(m.links[*].comment) ,2))
FROM `my-db` AS m
WHERE m.type = "links";