diff --git a/lib/document.js b/lib/document.js index 870a20261d1..b9f39a82c12 100644 --- a/lib/document.js +++ b/lib/document.js @@ -767,15 +767,14 @@ function init(self, obj, doc, opts, prefix) { } } else { // Retain order when overwriting defaults - if (doc.hasOwnProperty(i) && obj[i] !== void 0) { + if (doc.hasOwnProperty(i) && obj[i] !== void 0 && !opts.hydratedPopulatedDocs) { delete doc[i]; } if (obj[i] === null) { doc[i] = schemaType._castNullish(null); } else if (obj[i] !== undefined) { const wasPopulated = obj[i].$__ == null ? null : obj[i].$__.wasPopulated; - - if (schemaType && !wasPopulated) { + if ((schemaType && !wasPopulated) && !opts.hydratedPopulatedDocs) { try { if (opts && opts.setters) { // Call applySetters with `init = false` because otherwise setters are a noop diff --git a/test/model.hydrate.test.js b/test/model.hydrate.test.js index 9e4e9ce3d40..51358f08aa9 100644 --- a/test/model.hydrate.test.js +++ b/test/model.hydrate.test.js @@ -115,7 +115,6 @@ describe('model', function() { const company = { _id: new mongoose.Types.ObjectId(), name: 'Booster', users: [users[0]] }; const C = Company.hydrate(company, null, { hydratedPopulatedDocs: true }); - console.log('what is C', C); assert.equal(C.users[0].name, 'Val'); }); });