With the schema
var CommentSchema = new Schema({
text: { type: String },
replies: [{ type: mongoose.Schema.ObjectId, ref: 'Comment' }]
});
I have a query like
Comment.findById(req.params.id)
.populate({
path: 'replies',
model: 'Comment',
options: {
limit: 2
}
})
.exec(...)
Now I see no option to populate a limited number of elements in an array and keep the length of the array.
I thought about populating to a different "target" field, so that the original replies-array remains untouched (and therefore the information about the number of replies). But I think this option doesn't exist in mongoose populate().
The use-case can be seen on Youtube comments. A comment includes the number of replies, but only displays a limited number of them.