title: Mongoose - null references removed by populate?
body: <p>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:</p>
  
  <pre><code>new mongoose.Schema( {
      arr: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Schema2' } ],
  });
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>arr: [ null, null, null, null ]
  </code></pre>
  
  <p>This works great when generating the UI, using a Mongoose populate, until one field is actually set. If I for instance set <code>arr[2]</code> to reference a document, the array looks as expected using the mongo tool:</p>
  
  <pre><code>arr: [ null, null, ~ObjectID~, null ]
  </code></pre>
  
  <p>But using the same Mongoose populate as before removes the null fields, returning the array:</p>
  
  <pre><code>arr: [ {~Object of ObjectID~} ]
  </code></pre>
  
  <p>Which makes the UI generate just one cell of the expected four. The query I'm using looks somewhat like this:</p>
  
  <pre><code>Model1
      .findOne({ ... })
      .populate({
          path: 'arr',
          populate: {path: 'secondPopulateInSchema2'}
      })
      .exec(function(err, doc) {
          //Generate UI grid using doc.arr
      });
  </code></pre>
  
  <p>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!</p>
  
  <p>Regards,
  The Hult</p>