I'm trying to set a MongoDB database using Mongoose and I can't seem to make a self populate array work. Take a look at the code so you can get a better idea:
Here is the model code:
var Schema = _MONGOOSE.Schema;
var ModuleFields = Schema({
name: {type : String, default : '', trim : true},
label: {type : String, default : '', trim : true},
required: {type : Boolean, default: false},
type: {type : String, default: ''},
multiple: {type : Boolean, default: true},
predet: {type : Object, default: null},
fields:[{type: Schema.Types.ObjectId, ref:'ModuleFields'}],
createdAt : {type : Date, default : Date.now},
deletedAt : {type : Date}
});
ModuleFields.method({
uploadAndSave: function (cb) {
this.save(cb);
}
});
ModuleFields.static({
load: function (pName, cb) {
this.findOne({name : pName})
.populate('fields')
.exec(cb)
}
});
_MONGOOSE.model('ModuleFields', ModuleFields);
I added some data to the model, got no errors. Here is what I'm getting on the console:
> db.getCollectionNames()
[ "modulefields", "system.indexes" ]
> db.modulefields.findOne({name: 'video'})
{
"__v" : 1,
"_id" : ObjectId("52010bc4b2212caa01000001"),
"createdAt" : ISODate("2013-08-06T14:44:20.149Z"),
"deletedAt" : null,
"fields" : [
ObjectId("52010bd3b2212caa01000003")
],
"label" : "videos",
"multiple" : true,
"name" : "video",
"predet" : null,
"required" : true,
"type" : "custom"
}
So it seems to be ok, the reference is there in the array. When I look for the element it returns it, but the array is empty, here is a console.log of the data object that it finds:
Object {id: "52010bc4b2212caa01000001", name: "video", label: "videos", multiple: true, type: "custom"…}
fields: Array[0]
id: "52010bc4b2212caa01000001"
label: "videos"
multiple: true
name: "video"
required: true
type: "custom"
__proto__: Object
There seems to be something wrong with the populate but I can't find it. If some can help me or point me in the right direction to read about this I would really appreciate it.