From d3701d68f9817afe1e657dfc314b85dd8b61485e Mon Sep 17 00:00:00 2001 From: Mahi Sawner Date: Sat, 8 Nov 2025 01:09:44 +0530 Subject: [PATCH 1/2] perf: avoid cloning on save when minimize: false (Fixes #15694) --- lib/model.js | 9 +++++++-- package.json | 1 + types/document.d.ts | 2 +- types/index.d.ts | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/model.js b/lib/model.js index 3929413e767..241f8726ddc 100644 --- a/lib/model.js +++ b/lib/model.js @@ -357,7 +357,12 @@ Model.prototype.$__handleSave = function(options, callback) { } if (this.$isNew) { // send entire doc - const obj = this.toObject(saveToObjectOptions); + let obj; + if (this.$__schema.options.minimize === false) { + obj = this._doc; + } else { + obj = this.toObject(saveToObjectOptions); + } if ((obj || {})._id === void 0) { // documents must have an _id else mongoose won't know // what to update later if more changes are made. the user @@ -5194,4 +5199,4 @@ function _getContexts(hook) { * Module exports. */ -module.exports = exports = Model; +module.exports = exports = Model; \ No newline at end of file diff --git a/package.json b/package.json index 8261ba39fea..83cb09006f8 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "bson": "^6.10.4", "kareem": "2.6.3", "mongodb": "~6.20.0", + "mongoose": "^8.19.3", "mpath": "0.9.0", "mquery": "5.0.0", "ms": "2.1.3", diff --git a/types/document.d.ts b/types/document.d.ts index 8e5f5824a44..849d92a973d 100644 --- a/types/document.d.ts +++ b/types/document.d.ts @@ -302,4 +302,4 @@ declare module 'mongoose' { validateSync(pathsToValidate?: T | T[], options?: AnyObject): Error.ValidationError | null; validateSync(pathsToValidate?: pathsToValidate, options?: AnyObject): Error.ValidationError | null; } -} +} \ No newline at end of file diff --git a/types/index.d.ts b/types/index.d.ts index 8e8b5ad67f9..bac5572dc70 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -931,4 +931,4 @@ declare module 'mongoose' { export function skipMiddlewareFunction(val: any): Kareem.SkipWrappedFunction; export default mongoose; -} +} \ No newline at end of file From f2f5cdde5b02854b508065f9c210a61312999568 Mon Sep 17 00:00:00 2001 From: Mahi Sawner Date: Mon, 10 Nov 2025 08:36:35 +0530 Subject: [PATCH 2/2] updated package.json with 2 more commits --- lib/model.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/model.js b/lib/model.js index 241f8726ddc..d00ac2cd1e8 100644 --- a/lib/model.js +++ b/lib/model.js @@ -358,11 +358,11 @@ Model.prototype.$__handleSave = function(options, callback) { if (this.$isNew) { // send entire doc let obj; - if (this.$__schema.options.minimize === false) { - obj = this._doc; - } else { - obj = this.toObject(saveToObjectOptions); - } + // Always use toObject for serialization; control minimize via options + saveToObjectOptions = Object.assign({}, saveToObjectOptions, { + minimize: this.$__schema.options.minimize + }); + obj = this.toObject(saveToObjectOptions); if ((obj || {})._id === void 0) { // documents must have an _id else mongoose won't know // what to update later if more changes are made. the user @@ -5199,4 +5199,4 @@ function _getContexts(hook) { * Module exports. */ -module.exports = exports = Model; \ No newline at end of file +module.exports = Model; \ No newline at end of file