Skip to content

Mismatch between getChanges, modifiedPaths, and isModified methods #15522

@nikzanda

Description

@nikzanda

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.16.2

Node.js version

20.10.0

MongoDB server version

6.0.2

Typescript version (if applicable)

5.8.3

Description

When updating a model schema by adding a new field with a default value, retrieving any record and then executing the getChanges method returns a MongoDB instruction to update the record with the new field and its default value (a very nice feature!).

However, if I execute the modifiedPaths or isModified methods, they return an empty array and false, respectively. Why do these results mismatch?

Reproduction link here.

Image

Steps to Reproduce

  1. Create a model
  2. Insert a document
  3. Add a field with a default value to the schema
  4. Query the inserted document
  5. Call the getChanges, modifiedPaths, and isModified methods

Expected Behavior

My goal is to automatically update a document when new fields are added to the schema. For example, I would like to check for any changes by calling the isModified method, and if it returns true, execute save.

Metadata

Metadata

Assignees

No one assigned

    Labels

    helpThis issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions