3

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

TheHult
  • 31
  • 1
  • 2
  • 1
    Mongoose doesn't support populate with null refs, you can check this issue: https://github.com/Automattic/mongoose/issues/4161 – luisenrike May 26 '17 at 16:05
  • Thanks for the link! Still seems quite weird to me, but I will work around it! – TheHult May 27 '17 at 07:36
  • @TheHult have you found a way to work around it ? Staying at the exact same problem atm :/ – turbopasi Jan 29 '19 at 23:51
  • Huh actually looks like Mongoose is supporting it now. -> https://mongoosejs.com/docs/api.html#model_Model.populate // [options.retainNullValues=false] «boolean» by default, Mongoose removes null and undefined values from populated arrays. Use this option to make populate() retain null and undefined array entries - i will try it out. – turbopasi Jan 29 '19 at 23:55

1 Answers1

3

(2019) Mongoose does now support populating while keeping null and undefined array entries.

const options = {
  path: 'path',
  options: {
    retainNullValues: true 
  }
};

Model.findOne({...}).populate(options).exec(() => { ... });

Read here : https://mongoosejs.com/docs/api.html#model_Model.populate

turbopasi
  • 3,327
  • 2
  • 16
  • 43