With mongoose, I want to populate a property inside a subdocument of my main document. Here is my models:
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],
[...]
});
What I'm trying to do it's to get notifications with the device populated.
I tried this:
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);
}
});
And I got this
[{ device: 55634975d20f1f3903ff4325,
_id: 5564b1139ac484db0251b4a2,
createdAt: Tue May 26 2015 19:44:51 GMT+0200 (CEST) }]
Anyone can tell me what I'm doing wrong please ?? Thanks for your help guys :)