如何从数组到索引的末尾获取元素

问题描述 投票:1回答:1

样本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条消息,但我需要它直到数组末尾。

希望问题很清楚。

mongodb mongoose
1个回答
0
投票

您可以仅将$size用作最后一个$size参数:

$slice
© www.soinside.com 2019 - 2024. All rights reserved.