如何使用 N1QL 将单值对象列表转换为数组

How to single value object list to array with N1QL

我在 Couchbase 存储桶中有 json 个文件,看起来像这样

{
  "id":"10"
  "threadId": "thread1",
  "createdDate": 1553285245575,
}
{
  "id":"11"
  "threadId": "thread1",
  "createdDate": 1553285245776,
}
{
  "id":"12"
  "threadId": "thread2",
  "createdDate": 1553285245575,
}

我正在尝试创建一个查询,该查询基于按 threadId 的组和按 createdDate 的最新文档来获取文档。

我写了一个这样的 n1ql 查询,但它只是 return 这样的 documentId。

SELECT max([mes.createdDate,meta(mes).id])
from `messages`  as mes
group  by mes.threadId


result: 
    [
      {
        "": [
          1553285245776,
          "11"
        ]
      },
      {
        "": [
          1553285245737,
          "12"
        ]
      }
    ]

但我想要这样的结果

[{
  "id":"10"
  "threadId": "thread1",
  "createdDate": 1553285245575,
}
{
  "id":"11"
  "threadId": "thread1",
  "createdDate": 1553285245776,
}]

如有任何帮助,我们将不胜感激

SELECT m.*
FROM `messages` AS mes
WHERE mes.threadId IS NOT NULL
GROUP BY mes.threadId
LETTING m = MAX([mes.createdDate, mes])[1];

您可以使用以下索引和查询,它使用覆盖避免获取。

CREATE INDEX ix1 ON `messages`(threadId, createdDate DESC, id);
SELECT m.*
FROM `messages` AS mes
WHERE mes.threadId IS NOT NULL
GROUP BY mes.threadId
LETTING m = MAX([mes.createdDate,{mes.threadId,mes.createdDate, mes.id}])[1];