mongoose populate array of object id code example
Example 1: mongodb populate document
Story.
findOne({ title: 'Casino Royale' }).
populate('author').
exec(function (err, story) {
if (err) return handleError(err);
console.log('The author is %s', story.author.name);
});
Example 2: mongoos populate a ref
const storySchema = Schema({
authors: [{ type: Schema.Types.ObjectId, ref: 'Person' }],
title: String
});
const story = await Story.findOne({ title: 'Casino Royale' }).populate('authors');
story.authors;
Example 3: mongoose populate array of ids
Use the name of the schema path instead of the collection name:
Conversation.findOne({ _id: myConversationId})
.populate('recipients')
.exec(function(err, conversation){
});