diff --git a/lib/document.js b/lib/document.js index 7000747793..1015824614 100644 --- a/lib/document.js +++ b/lib/document.js @@ -3780,7 +3780,7 @@ Document.prototype.$__handleReject = function handleReject(err) { */ Document.prototype.$toObject = function(options, json) { - let defaultOptions = { + const defaultOptions = { transform: true, flattenDecimals: true }; @@ -3793,7 +3793,7 @@ Document.prototype.$toObject = function(options, json) { const schemaOptions = this.$__schema && this.$__schema.options || {}; // merge base default options with Schema's set default options if available. // `clone` is necessary here because `utils.options` directly modifies the second input. - defaultOptions = { ...defaultOptions, ...baseOptions, ...schemaOptions[path] }; + Object.assign(defaultOptions, baseOptions, schemaOptions[path]); // If options do not exist or is not an object, set it to empty object options = utils.isPOJO(options) ? { ...options } : {}; @@ -3865,19 +3865,17 @@ Document.prototype.$toObject = function(options, json) { options.minimize = _minimize; cloneOptions._parentOptions = options; - cloneOptions._skipSingleNestedGetters = false; - - const gettersOptions = Object.assign({}, cloneOptions); - gettersOptions._skipSingleNestedGetters = true; + cloneOptions._skipSingleNestedGetters = false; // remember the root transform function // to save it from being overwritten by sub-transform functions const originalTransform = options.transform; let ret = clone(this._doc, cloneOptions) || {}; + cloneOptions._skipSingleNestedGetters = true; if (options.getters) { - applyGetters(this, ret, gettersOptions); + applyGetters(this, ret, cloneOptions); if (options.minimize) { ret = minimize(ret) || {}; @@ -3885,7 +3883,7 @@ Document.prototype.$toObject = function(options, json) { } if (options.virtuals || (options.getters && options.virtuals !== false)) { - applyVirtuals(this, ret, gettersOptions, options); + applyVirtuals(this, ret, cloneOptions, options); } if (options.versionKey === false && this.$__schema.options.versionKey) {