mongoose populate field code example
Example 1: mongoose populate
Story.
findOne({ title: /casino royale/i }).
populate('author', 'name').
exec(function (err, story) {
if (err) return handleError(err);
console.log('The author is %s', story.author.name);
console.log('The authors age is %s', story.author.age);
});
Example 2: how to use mongoose populate
{ _id: 59ab1c92ea84486fb4ba9f28, username: 'JD', posts: [ { _id: 59ab1b43ea84486fb4ba9ef0, content: "Is it dark out?" },{ _id: 59ab1b43ea84486fb4ba9ef1, content: "Hey anyone got a cup of sugar?" } ] }