mongoose populate to array of ObjectIds

48 views Asked by At

This is my schema

friends: {
    type: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    }],
    required:false,
    validate: [arrayLimit, '{PATH} exceeds the limit of 3']
  }

I want to find all the user related Like populate but with multi user and I do not want all the user fields just the relvant for example

const responses = await Response.find({}).populate("friends", {name:1, username:1})
1

There are 1 answers

0
J.F. On

According to mongoose docs yo can do something like:

const responses = await Response
  .find({})
  .populate({
    path: 'friends',
    select: 'name username -_id'
  })