我试图了解如何从同一集合中同时填充两个引用。 我有这些模型:
const bookingSchema = new Schema( {
created_at: {
type: String,
default: moment().format('llll')
},
// When service start in timestams seconds
date: {
type: Number
},
package: {
type: Schema.Types.ObjectId,
ref: 'Package',
required: [ true, 'A reference to a package must exist']
},
status: {
type: String // ['pending', 'confirmed', 'finished']
},
});
const packageSchema = new Schema( {
title: {
type: String
},
profile: {
type: Schema.Types.ObjectId,
ref: 'Profile',
required: [ true, 'A reference to a profile must exist']
},
service: {
type: Schema.Types.ObjectId,
ref: 'Service',
required: [ true, 'A reference to a service must exist']
}
]
});
这是查询,我只能获取配置文件信息,但是服务始终为NULL,但是每个Package都有一个服务。 可能我在语法上犯了错误吗?如果看到的话,用我要附加到查询结果,配置文件和服务的引用填充一个数组,配置文件正确无误,问题出在服务上。
return Booking.find( {'package': {$in: ids}} )
.populate({
path: 'package',
model: 'Package',
match: {'profile' : { $exists: true}},
select : 'profile title service language',
populate : [
{
path : 'service',
model: 'Service',
select : '_id name'
},
{
path : 'profile',
model: 'Profile',
select : '_id name',
populate: {
path: 'user',
model: 'User',
select: 'role'
}
}
]
})
.sort( { date: -1 } )
.exec();