使用Mongodb,猫鼬,nodejs,express ....
我有这样的结构...
const EventSchema = new mongoose.Schema({
eventDate: {
type: Date
},
attendees: [
{
type: mongoose.Schema.ObjectId,
ref: 'Person'
}
],
staff: [
{
person: {
type: mongoose.Schema.ObjectId,
ref: 'User'
},
department: {
type: String,
enum: ['photography', 'data-entry', 'mgmt', 'other']
}
}
]
});
我知道如何填充(“与会者部门”)。正常工作,并返回两者的所有数据。但是,由于我实际上是在处理多个填充,因此如何选择应为与会者和部门检索的特定字段?
答案 0 :(得分:1)
您可以按以下方式编写查询,以填充多个值并仅返回所需的特定字段
Event.find().populate([{
path: 'attendees',
model: 'Person',
select: '_id name profile_pic' //Fields you want to return in this populate
}, {
path: 'staff.person',
model: 'User',
select: '_id name profile_pic' //Fields you want to return in this populate
}])