使用Mongodb,mongoose,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']
}
}
]
});我知道如何填写(‘与会者部门’)。这样做可以很好地工作,并返回这两个函数的所有数据。但是,由于我主要处理的是多个填充,如何选择应该为与会者和部门检索的特定字段?
发布于 2020-01-13 18:06:54
您可以按如下方式编写查询,以填充多个值并仅返回所需的特定字段
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
}])https://stackoverflow.com/questions/59516242
复制相似问题