how populate works in mongoose 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

{   _id: 59ab1c92ea84486fb4ba9f28,  username: 'JD',  posts:    [       {         _id: 59ab1b43ea84486fb4ba9ef0,        content: "Is it dark out?"      },{        _id: 59ab1b43ea84486fb4ba9ef1,        content: "Hey anyone got a cup of sugar?"      }    ]  }

Tags:

Misc Example