Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use .populate() in Mongoose to select specific fields, you can pass an object of options as the second argument to the .populate() method. This object should have a 'select' key set to a space-separated list of the fields you want to select.

For example:

User.findOne({ name: 'John' })
  .populate({
    path: 'posts',
    select: 'title date'
  })
  .exec(function(err, user) {
    if (err) throw err;
    console.log(user.posts);
  });

In this example, we are selecting only the 'title' and 'date' fields from the 'posts' collection while populating it.