Answers for "mongoose populate select fields"

1

mongoose populate selected fields

Model
.find(query)
.populate({
  path: 'key_with_ref', 
  select: ['field_1', 'field_2']
});
Posted by: Guest on November-17-2021
1

Return certain fields with populate from mongoose

Model
.find(query)
.populate({
  path: 'key_with_ref',
  model: 'model_name',
  select: { 'field_name': 1,'field_name':1},
})
 
-OR-

Model
.find(query)
.populate({
  path: 'key_with_ref',
  model: 'model_name',
  select: 'field_name, field_name',
})
Posted by: Guest on May-28-2021
0

mongoos populate a ref

const storySchema = Schema({
  authors: [{ type: Schema.Types.ObjectId, ref: 'Person' }],
  title: String
});

// Later

const story = await Story.findOne({ title: 'Casino Royale' }).populate('authors');
story.authors; // `[]`
Posted by: Guest on April-28-2020

Code answers related to "mongoose populate select fields"

Browse Popular Code Answers by Language