使用貓鼬,我想在我的主文檔的子文檔中填充屬性。 這裏是我的模型:貓鼬+在子文檔中填充屬性
var device = {
deviceId: {type: String, required: true},
number: {type: Number, default: 0}
};
var notification = new Schema({
createdAt: {type: Date, default: Date.now},
device: {type: Schema.ObjectId, ref: 'Device'}
});
var clientSchema = new Schema({
[...]
information: {
code: {type: String, required: true},
name: {type: String, required: true}
},
notifications: [notification],
[...]
});
我試圖做到這一點是得到與填充設備的通知。
我嘗試這樣做:
clientModel.findOne({"information.code": id})
.populate('notifications')
.populate('notifications.device')
.exec(function(err, client) {
if (err) {
console.log(err);
next(err, null);
}
if (client) {
console.log(client.notifications);
next(null, client.notifications);
}
});
而且我得到了這個
[{ device: 55634975d20f1f3903ff4325,
_id: 5564b1139ac484db0251b4a2,
createdAt: Tue May 26 2015 19:44:51 GMT+0200 (CEST) }]
任何人都可以告訴我,我做錯了什麼?請 感謝您的幫助:)
[Mongoose:deep population(populate populated field)](http://stackoverflow.com/questions/18867628/mongoose-deep-population-populate-a-populated-field) – laggingreflex