diff --git a/lib/helpers/document/compile.js b/lib/helpers/document/compile.js index 0868f24516c..1a2a0357d8b 100644 --- a/lib/helpers/document/compile.js +++ b/lib/helpers/document/compile.js @@ -2,6 +2,7 @@ const documentSchemaSymbol = require('../../helpers/symbols').documentSchemaSymbol; const get = require('../../helpers/get'); +const internalToObjectOptions = require('../../options').internalToObjectOptions; const utils = require('../../utils'); let Document; @@ -142,6 +143,8 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) { // Convert top-level to POJO, but leave subdocs hydrated so `$set` // can handle them. See gh-9293. v = v.$__parent.get(path); + } else if (v instanceof Document && !v.$__isNested) { + v = v.toObject(internalToObjectOptions); } const doc = this.$__[scopeSymbol] || this; doc.$set(path, v);