mongoose 填充到 ObjectId 数组

问题描述 投票:0回答:1

这是我的架构

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

我想找到所有与填充但具有多用户相关的用户 我不希望所有用户字段只是相关的 例如

const responses = await Response.find({}).populate("friends", {name:1, username:1})
mongodb mongoose mongoose-populate
1个回答
0
投票

根据 mongoose 文档 你可以这样做:

const responses = await Response
  .find({})
  .populate({
    path: 'friends',
    select: 'name username -_id'
  })
© www.soinside.com 2019 - 2024. All rights reserved.