填充引用另一个Model的子文档的字段

时间:2016-10-04 13:44:37

标签: node.js mongodb mongoose

我在填充另一个Model中作为子文档存在的对象时遇到了困难。

var BuildingSchema = new mongoose.Schema({
address: String,
country: String
});

var CompanySchema = new mongoose.Schema({
name: String,
buildings: [BuildingSchema],
});

var MaintenanceSchema = new mongoose.Schema({
due: Date,
company: { type: mongoose.Schema.Types.ObjectId, ref: 'Company' },      
building: { type: mongoose.Schema.Types.ObjectId, ref: 'Building' },    
});

查询:

return Maintenance.find()
.populate('company building')
.exec()
.then(respondWithResult(res))
.catch(handleError(res));

我可以确认Mongodb中的维护条目确实包含存储在匹配公司中的建筑物的正确对象ID。

find()查询填充"公司"正确但返回null为"构建"。

我猜测Mongoose没有查看公司的子文档来查找对象id的匹配项。

我该怎么做?

0 个答案:

没有答案
相关问题