-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot patch a sub-document with destructuring #14269
Comments
const mongoose = require('mongoose');
const addressSchema = new mongoose.Schema(
{
street: String,
city: String,
state: String,
zip: Number,
},
{ _id: false }
);
const personSchema = new mongoose.Schema({
name: String,
age: Number,
address: addressSchema,
});
const personModel = mongoose.model("Person", personSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const person = new personModel({
name: "John",
age: 42,
address: {
street: "123 Fake St",
city: "Springfield",
state: "IL",
zip: 12345,
},
});
await person.save();
person.address = {
...person.address,
zip: 54321,
};
await person.save();
console.log('what is person', person);
}
run(); |
Looks to be a slightly different case of #11522. I would strongly advise you do
We will put in a PR to make the OP's code work, but we recommend against using it. |
@vkarpov15 I would agree with you that I'm not writing React here. However, I am writing javascript here and destructuring has been apart of the spec since es6 was introduced in 2015. There are real-world use cases where destructuring makes more sense than using the alternatives provided above. Although this is a contrived example. Is you make person.address optional and all address fields optional in the above example, destructuring helps consolidate code like:
Obviously, this is just an example, but with more complex code bases, this pattern surely applies. |
fix(document): handle setting nested path to spread doc with extra properties
I agree with @a88zach, it is not obvious that upgrading from Mongoose 5 to 8 will break a core JavaScript functionality. That fix actually makes it worse because now it kind of works, but still not how everyone, except Mongoose developers, would expect it. |
For the following code:
The more concise way in Mongoose would be I agree that |
Prerequisites
Mongoose version
8.1.0
Node.js version
20.11.0
MongoDB server version
7.x
Typescript version (if applicable)
5.3.3
Description
When patching a sub-document with destructuring, the changes are not saved correctly.
However, after saving (and even before the save is called) the person.address.zip is still the old value.
Steps to Reproduce
Cone this repo: https://github.com/a88zach/mongoose-bug
run
$ yarn test
Notice the 1 test that is successful and the 1 test that fails. The one that fails is using destructuring to patch the sub document
Expected Behavior
The sub-document should be updated with the existing information plus any overrides based on the patch
The text was updated successfully, but these errors were encountered: