Skip to content

mongoose 5.4.20 does not overwrite all properties in a nested subdocument. #7660

@ozomer

Description

@ozomer

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Trying to overwrite (override) a field that contains a nested subdocument updates some nested fields but not another.

If the current behavior is a bug, please provide the steps to reproduce.

mongoose = require('mongoose');

MySchema = new mongoose.Schema({
  innerValue1: new mongoose.Schema({
    aaa: {
      type: String,
    },
    zzz: {
      type: String,
    },
    geometry: new mongoose.Schema({ // using a subdocument here makes geometry optional
      title: {
        type: String,
      },
      coordinates: {
        type: [Number],
      },
    }, {
      _id: false,
      id: false,
    }),
  }, {
    _id: false,
    id: false,
  }),
});

MySchemaModel = mongoose.model('MySchemaModel', MySchema);

doc = new MySchemaModel({
  innerValue1: {
    aaa: 'aaa1',
    zzz: 'zzz1',
    geometry: {
      title: 'Point1',
      coordinates: [1, 1]
    }
  },
});

doc.innerValue1 = {"zzz":"zzz2","geometry":{"coordinates":[2,2],"title":"Point2"},aaa:"aaa2"}

What is the expected behavior?

The expected value would be a document with innerValue1 equals {"zzz":"zzz2","geometry":{"coordinates":[2,2],"title":"Point2"},aaa:"aaa2"}.
What I get is:

// JSON.stringify(doc.innerValue1.toObject())
'{"zzz":"zzz1","geometry":{"coordinates":[1,1],"title":"Point1"},"aaa":"aaa2"}'

Only one field (aaa) has changed.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

NodeJS 10.14.1
Mongoose 5.4.20
No mongodb connection.

I've tested the same code on mongoose 5.4.19 and it works as expected. This bug must have been introduced between 5.4.19 and 5.4.20. If this is a known side effect of other changes, it should be a major/minor version change, not a patch version change (19 to 20), because it is not backward compatible.

Thanks

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