Mongoose, find, return specific properties

You use projection. The first example in the mongoose query docs has a projection operation tucked in.

NB: not real code b/c I highlighted the important bits with triple stars

// find each person with a last name matching 'Ghost', ***selecting the `name` and `occupation` fields***
Person.findOne({ 'name.last': 'Ghost' }, ***'name occupation'***, function (err, person) {
  if (err) return handleError(err);
  console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host.
})

The Person schema isn’t specified but I think the example is clear enough.

Leave a Comment