样本Mongo文档
chat = { _id: someid, messages: [{text: 'aaa'}, {text: 'bbb'}, {text: 'ccc'}, {text: 'ddd'}] }
我需要提取从索引1开始到数组末尾的消息。谢谢。
到目前为止尝试过的代码:
let theIndex = 1;
Model.aggregate(
{ $match: condition },
{ $project: { 'chat': { $slice: ['$chat.messages', theIndex, 25] } } }
)
这将给我25条消息,但我需要它直到数组末尾。
希望问题很清楚。
您可以仅将$size
用作最后一个$size
参数:
$slice