mongoose populate inside populate 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: mongoose create populate response
let user = await User.create({ ... })
user = await user.populate('company').execPopulate()
Example 3: 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?" } ] }
Example 4: how to use mongoose populate
{ _id: 59ab1c92ea84486fb4ba9f28, username: JD, posts: [ "59ab1b43ea84486fb4ba9ef0", "59ab1b43ea84486fb4ba9ef1" ]}
Example 5: how to use mongoose populate
function getUserWithPosts(username){ return User.findOne({ username: username }) .populate('posts').exec((err, posts) => { console.log("Populated User " + posts); })}