我在查询数组中的对象时遇到了问题。
我有这个示例文档
{
...
status: 'active',
current: { ... },
history: [
{
id: '73bae187-1101-4fb3-a71a-2bbf90026eb3',
date: '2017-03-28T09:32:22.884Z',
content: 'This is second content'
},
{
id: 'd6a6c63d-42db-4ef5-88e9-616cfe539a57',
date: '2017-03-25T09:32:22.884Z',
content: 'This is first content'
},
{
id: '3fbdb846-2b55-4ff8-8960-86997ef31556',
schedule: '1970-01-01T00:00:00.000Z',
content: 'This is a very old content'
}
]
}
我想直接从我想要应用日期过滤器的历史数组子文档中进行查询。
是否可以在 n1ql 中检索仅包含满足条件的对象的历史数组?
我可以应用一个限制来控制数组中返回对象的数量吗?
我使用拼接[0:$1]
尝试了一些查询,其中limit是输入,但是当限制大于数组大小时,它不起作用。
感谢您的帮助。
答案 0 :(得分:4)
尝试以下任一方法:
SELECT h[0:least(ARRAY_LENGTH(h), $arraylimit)] As h FROM default As d
LET h = ARRAY v FOR v IN d.history WHEN v.id IN [ 'xyz', 'pqr'] END;
或者:
SELECT h[0:least(ARRAY_LENGTH(h),$limit)] As h (SELECT ARRAY_AGG(h) As h
FROM default As d UNNEST d.history As h WHERE h.id IN [ 'xyz', 'pqr'] GROUP BY d) As q;