mongoose populate instane code example
Example 1: mongoose populate
Story.
findOne({ title: /casino royale/i }).
populate('author', 'name'). // only return the Persons name
exec(function (err, story) {
if (err) return handleError(err);
console.log('The author is %s', story.author.name);
// prints "The author is Ian Fleming"
console.log('The authors age is %s', story.author.age);
// prints "The authors age is null'
});
Example 2: how to use mongoose populate
function getUserWithPosts(username){ return User.findOne({ username: username }) .populate('posts').exec((err, posts) => { console.log("Populated User " + posts); })}