Return certain fields with .populate() from Mongoose
I'm not completely clear on what you mean by "returning a field", but you can use a lean()
query so that you can freely modify the output, then populate both fields and post-process the result to only keep the field you want:
.lean().populate('user', 'email.address facebook.address')
.exec(function (err, subscription){
if (subscription.user.email.address) {
delete subscription.user.facebook;
} else {
delete subscription.user.email;
}
});
If you only want a few specific fields to be returned for the populated documents, you can accomplish this by passing the field name syntax as the second argument to the populate method.
Model
.findOne({ _id: 'bogus' })
.populate('the_field_to_populate', 'name') // only return the Persons name
...
See Mongoose populate field selection