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
I experience the same bug. I think this commit introduced it: bb78de76e10faf7514fefe423dfcdf5d2e2ec215
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.
I have the same problem using Object.assign()
Same problem here using Object.assign(). This is not a properly change for a minor update.
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.
Confirmed here as well, downgrading to 5.4.19 fix the issue
@vkarpov15 Issue #7681 has a nice simple repro script...
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
@vkarpov15
could you add confirmed-bug
and has repro script
labels?
Fix will be in 5.5.1, thanks for reporting
@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 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.
Most helpful comment
I have the same problem using Object.assign()