Categories
JavaScript Answers

How to return certain fields with .populate() from Mongoose?

Spread the love

Sometimes, we want to return certain fields with .populate() from Mongoose.

In this article, we’ll look at how to return certain fields with .populate() from Mongoose.

How to return certain fields with .populate() from Mongoose?

To return certain fields with .populate() from Mongoose, we can call populate with the fields we want to populate.

For instance, we write

Model
  .findOne({
    _id
  })
  .populate('field', 'name')

to call populate with 'field' and 'name' to populate the field and name fields in the returned documents.

Conclusion

To return certain fields with .populate() from Mongoose, we can call populate with the fields we want to populate.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *