diff --git a/lib/helpers/populate/assignRawDocsToIdStructure.js b/lib/helpers/populate/assignRawDocsToIdStructure.js index bb3d988a68e..b84f7131ca2 100644 --- a/lib/helpers/populate/assignRawDocsToIdStructure.js +++ b/lib/helpers/populate/assignRawDocsToIdStructure.js @@ -1,5 +1,7 @@ 'use strict'; +const modelSymbol = require('../symbols').modelSymbol; + module.exports = assignRawDocsToIdStructure; /*! @@ -63,8 +65,10 @@ function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, re } else { newOrder.push(doc); } + } else if (id != null && id[modelSymbol] != null) { + newOrder.push(id); } else { - newOrder.push(nullIfNotFound ? null : id); + newOrder.push(options.retainNullValues || nullIfNotFound ? null : id); } } else { // apply findOne behavior - if document in results, assign, else assign null diff --git a/test/model.populate.test.js b/test/model.populate.test.js index 5d8750d5c66..eaf72afdbf7 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -8795,6 +8795,9 @@ describe('model: populate:', function() { }); assert.equal(doc.troops.length, 4); assert.equal(doc.troops[0], null); + assert.equal(doc.troops[1].name, 'Card 2'); + assert.equal(doc.troops[2].name, 'Card 3'); + assert.equal(doc.troops[3].name, 'Card 4'); }); }); });