I am trying to populate a object myObj. After saving values to 'a' object, I am able to see the data for 'myObj' in the db. But when I query for the obj 'a', its not pulling the 'myObj' data. My Schema is as below.
var a= new Schema({
b: 'Date',
c: {
d: {
type: Schema.ObjectId,
ref: 'Student'
},
myObj: {
type: [{
type: Schema.ObjectId,
ref: 'Exam'
}],
select: true
},
}
});
Exam Schema
var ExamSchema= new Schema({
name: String,
subject: {
type: [{
name: String,
marks: Number
}],
select: false
}
});
My querying method:
exports.a = function(req, res, next, id) {
A //model of 'a'
.findOne({
_id: id
})
.populate('c.d c.myObj')
.exec(function(err, aObj) {
if (err) return next(err);
if (!aObj) return next(new Error('Failed to load ' + id));
req.a = aObj;
next();
});
};
Expected Output:
var a = new Schema({
b: "2014-07-10T02:30:00.005Z",
,
c: {
d: {
name: 'Hari',
age: 10
},
myObj: {
[{
name: 'Quaterly',
subject: {
[{
name: 'English',
marks: 100
}, {
name: 'Maths',
marks: 90
}, {
name: 'Science',
marks: 100
}],
select: false
}
}, {
name: 'Half-Yearly',
subject: {
[{
name: 'English',
marks: 100
}, {
name: 'Maths',
marks: 90
}, {
name: 'Science',
marks: 100
}],
select: false
}
}],
select: true
},
}
});
**Actual Output:**
var a = new Schema({
b: "2014-07-10T02:30:00.005Z",
,
c: {
d: {
name: 'Hari',
age: 10
},
myObj: []
}
});
Please let me know where I am wrong. Thanks.
populate('c.d c.myObj')?