Skip to content

Conversation

@mahi656
Copy link

@mahi656 mahi656 commented Nov 7, 2025

Description

Optimize $__handleSave() performance by skipping toObject() call when minimize: false

Problem

When saving a new document (isNew: true), Mongoose always calls toObject(saveToObjectOptions) to prepare the document for insertion. However, when minimize: false, this call is unnecessary because:

  • The MongoDB driver clones the document internally before insertion
  • The driver automatically applies toBSON() transformations
  • The toObject() call adds significant overhead by cloning, transforming, and processing the entire document structure

Solution

Skip the toObject() call when schema.options.minimize === false and directly use this._doc instead. The MongoDB driver will handle all necessary cloning and transformations.

Performance Impact

This optimization significantly improves save performance for schemas with minimize: false by eliminating an expensive document transformation step. The performance gain is especially noticeable for:

  • Large documents with many fields
  • Documents with nested subdocuments
  • High-throughput applications performing many inserts

Testing

✅ All existing tests pass:

  • Save-related tests: 118 passing
  • Minimize-related tests: 17 passing
  • isNew-related tests: 15 passing

Verified the optimization works correctly:

  • ✅ MongoDB driver handles cloning correctly
  • ✅ Tested with minimize: false schemas - optimization applied
  • ✅ Tested with minimize: true schemas - no behavior change
  • ✅ Tested with schemas where minimize is undefined - no behavior change

Backward Compatibility

✅ This change is fully backward compatible:

  • Only affects documents being inserted (isNew: true)
  • Only applies optimization when minimize: false
  • All other cases use existing toObject() logic

Issue

Fixes #15694

@vkarpov15 vkarpov15 requested a review from Copilot November 9, 2025 20:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes the save operation for new documents when minimize is set to false by bypassing the toObject() call and directly using the internal _doc object. It also includes minor formatting corrections in TypeScript definition files.

  • Uses this._doc directly when minimize === false for new document inserts to avoid unnecessary toObject() processing
  • Adds newlines to the closing braces of TypeScript declaration files for consistency

Reviewed Changes

Copilot reviewed 1 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/model.js Adds conditional logic to use this._doc directly when minimize === false during new document saves, bypassing toObject()
types/index.d.ts Adds newline before closing brace for formatting consistency
types/document.d.ts Adds newline before closing brace for formatting consistency
Comments suppressed due to low confidence (1)

lib/model.js:378

  • The $__version() method expects to mutate the obj parameter by setting the version key via setDottedPath(delta, key, 0) (line 681 in $__version implementation). When obj is set to this._doc directly, this mutation affects the document's internal state, but may not properly set nested paths that need to be persisted. Additionally, obj is expected to be a plain object that will be inserted into MongoDB, not the internal _doc which may contain special Mongoose metadata.
    this.$__version(true, obj);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Author

@mahi656 mahi656 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done this export correctly

@vkarpov15
Copy link
Collaborator

closing this re: #15694 (comment)

@vkarpov15 vkarpov15 closed this Nov 10, 2025
@mahi656 mahi656 deleted the mongodb branch November 11, 2025 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance: avoid cloning when saving document if minimize: false

2 participants