I have a problem I can't figure out how to solve. I've got a Mongoose model which contains an array of references, which are to be populated. Something like this:
new mongoose.Schema( {
arr: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Schema2' } ],
});
The array is representing a grid in the UI of my app, and is by design of a fixed length. The idea is to let the fields of the array be null if they are not yet set to anything, ie. the array looks like this when nothing is set:
arr: [ null, null, null, null ]
This works great when generating the UI, using a Mongoose populate, until one field is actually set. If I for instance set arr[2]
to reference a document, the array looks as expected using the mongo tool:
arr: [ null, null, ~ObjectID~, null ]
But using the same Mongoose populate as before removes the null fields, returning the array:
arr: [ {~Object of ObjectID~} ]
Which makes the UI generate just one cell of the expected four. The query I'm using looks somewhat like this:
Model1
.findOne({ ... })
.populate({
path: 'arr',
populate: {path: 'secondPopulateInSchema2'}
})
.exec(function(err, doc) {
//Generate UI grid using doc.arr
});
Again, works as expected (by me) for an array filled with null, but not when at least one of the fields has a reference. I have no idea why, I´ve been fiddling around with options and trying to figure out a clean way of solving it (without the use of external loops and "populates"). Any help would be much appreciated!
Regards, The Hult
via TheHult
No comments:
Post a Comment