mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

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

Open ozomer opened this issue 7 years ago • 12 comments

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

ozomer avatar Mar 29 '19 09:03 ozomer

I experience the same bug. I think this commit introduced it: bb78de76e10faf7514fefe423dfcdf5d2e2ec215

Galste avatar Mar 29 '19 13:03 Galste

Confirmed.

When using: doc.set({ ...req.body }) and document has some subdoc, it won't change all fields.

In my case that was required field in nested Schema, so it threw an error.

vuckookv3 avatar Mar 29 '19 14:03 vuckookv3

I have the same problem using Object.assign()

marcvincenti avatar Mar 29 '19 14:03 marcvincenti

Same problem here using Object.assign(). This is not a properly change for a minor update.

cluny85 avatar Mar 29 '19 15:03 cluny85

I can confirm, that this was introduced with 5.4.20. @vkarpov15 this should be in for the 5.4.22 milestone since it's a serious problem (breaking pretty much any app using single nested subdocuments) which renders all improvements made since 5.4.20 pointless.

marcelh-gh avatar Apr 02 '19 21:04 marcelh-gh

Confirmed here as well, downgrading to 5.4.19 fix the issue

paglias avatar Apr 04 '19 10:04 paglias

@vkarpov15 Issue #7681 has a nice simple repro script...

marcelh-gh avatar Apr 04 '19 12:04 marcelh-gh

I have even a simpler repro script:

const mongoose = require('mongoose');

const headSchema = new mongoose.Schema({
  color: String,
  size: Number,
}, {
  _id: false,
  id: false,
});

const dogSchema = new mongoose.Schema({
  head: headSchema,
}, {
  strict: true,
});
const Dog = mongoose.model('Dog', dogSchema);

const dog = new Dog({
  head: {
    color: 'black',
    size: 5,
  },
});

dog.set({
  head: {
    color: 'white',
    size: 13,
  },
});
// for [email protected], 5.4.21 and 5.4.22: { color: 'black', size: 13 }
// for [email protected]: { color: 'white', size: 13 }
console.log(dog.head);

btw. a few hours ago mongoose released a new version - 5.4.22 but this issue is still broken

oprogramador avatar Apr 05 '19 02:04 oprogramador

@vkarpov15

could you add confirmed-bug and has repro script labels?

oprogramador avatar Apr 05 '19 02:04 oprogramador

Fix will be in 5.5.1, thanks for reporting

vkarpov15 avatar Apr 09 '19 18:04 vkarpov15

@vkarpov15

IMO the fix should be in one of the 5.4.x versions because it's a bug, not a feature. Anyway thanks.

oprogramador avatar Apr 10 '19 14:04 oprogramador

@oprogramador I agree that would be ideal, but we didn't get started working on this issue until after 5.5 was released. We generally don't maintain previous minor releases - we patch Mongoose 4.x as necessary because there are known backwards breaking changes, but there should be nothing preventing you from upgrading from 5.4.19 to 5.5.1.

vkarpov15 avatar Apr 11 '19 16:04 vkarpov15