diff --git a/lib/document.js b/lib/document.js index c6d7b70ba98..6ede708aab2 100644 --- a/lib/document.js +++ b/lib/document.js @@ -311,6 +311,7 @@ Document.prototype.init = function(doc, opts, fn) { } this.isNew = false; + this.$init = true; // handle docs with populated paths // If doc._id is not null or undefined diff --git a/lib/model.js b/lib/model.js index 05a068bad8e..4f22827ba05 100644 --- a/lib/model.js +++ b/lib/model.js @@ -19,6 +19,7 @@ var cast = require('./cast'); var castUpdate = require('./services/query/castUpdate'); var discriminator = require('./services/model/discriminator'); var isPathSelectedInclusive = require('./services/projection/isPathSelectedInclusive'); +var mpath = require('mpath'); var parallel = require('async/parallel'); var util = require('util'); var utils = require('./utils'); @@ -3190,7 +3191,14 @@ function assignVals(o) { if (o.isVirtual && !o.justOne && !Array.isArray(rawIds[i])) { rawIds[i] = [rawIds[i]]; } - utils.setValue(o.path, rawIds[i], docs[i], setValue); + + if (o.isVirtual && docs[i].constructor.name === 'model' && docs[i].$init) { + // If virtual populate and doc is already init-ed, need to walk through + // the actual doc to set rather than setting `_doc` directly + mpath.set(o.path, rawIds[i], docs[i]); + } else { + utils.setValue(o.path, rawIds[i], docs[i], setValue); + } } }