这是我的架构
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})
根据 mongoose 文档 你可以这样做:
const responses = await Response
.find({})
.populate({
path: 'friends',
select: 'name username -_id'
})