1

Sample Mongo Document

chat = { _id: someid, messages: [{text: 'aaa'}, {text: 'bbb'}, {text: 'ccc'}, {text: 'ddd'}] }

I need to extract messages starting from index 1 till the end of the array. Thanks.

Codes tried so far:

let theIndex = 1;
Model.aggregate(
  { $match: condition },
  { $project: { 'chat': { $slice: ['$chat.messages', theIndex, 25]  } } }
)

This will give me 25 messages, but i need it to be till the end of the array.

Hope the question is clear.

1
  • Still need clarification on what you want. Also post the code what you have tried so far Commented Jan 3, 2020 at 9:26

1 Answer 1

1

You can just use $size as a last $slice parameter:

let theIndex = 1;
Model.aggregate(
   { $match: condition },
   { $project: { 'chat': { $slice: ['$chat.messages', theIndex, { $size: '$messages' }]  } } }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.