Skip to content

Unmodified fields being marked as modified. #11913

Closed
@jjacksonjc

Description

@jjacksonjc

Prerequisites

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

Mongoose version

6.3.5

Node.js version

16.x

MongoDB server version

5.x

Description

When a child field is modified, all children sharing a common parent are getting marked as modified, whether or not they were actually modified. This is a deviation from mongoose v5 and lower behavior and seems to be unintentional since it doesn't appear that any unit tests are checking for this case.

Steps to Reproduce

With mongo listening on 27017 run the following script and observe the output.

'use strict';

const mongoose = require('./lib');

const person = new mongoose.Schema({
  name: {
    first: String,
    last: String
  },
  haircolor: String,
  eyecolor: String
});

const Person = mongoose.model('Person', person);

(async function() {
  await mongoose.connect('mongodb://localhost:27017/example');

  const h = {
    name: {
      first: 'bob',
      last: 'jones'
    },
    haircolor: 'brown',
    eyecolor: 'brown'
  };

  const human = new Person(h);

  await human.save();

  h.name.first = 'Larry';
  human.set(h);

  console.log(`expect 'name,name.first': ${human.modifiedPaths({ includeChildren: true })}`);
  console.log(`expect 'false': ${human.isModified('haircolor')}`);
  console.log(`expect 'false': ${human.isModified('eyecolor')}`);
  console.log(`expect 'true': ${human.isModified('name.first')}`);
  console.log(`expect 'false': ${human.isModified('name.last')}`);

  await mongoose.disconnect();
})();

Expected Behavior

In the above example name.last should not be marked as modified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions