Comments (5)
@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:
if (person.address) {
person.address.zip = 54321;
} else {
person.address = {
zip: 54321;
}
}
Obviously, this is just an example, but with more complex code bases, this pattern surely applies.
from mongoose.
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();
from mongoose.
Looks to be a slightly different case of #11522. I would strongly advise you do person.address.zip = 54321;
or person.address.set('zip', 54321)
instead of using the spread operator. You aren't writing React here, person.address = { ...person.address, zip: 54321 }
is just an unnecessary shallow clone that hurts performance. The other workaround is to do the following:
person.address = {
...person.address.toObject(),
zip: 54321,
};
We will put in a PR to make the OP's code work, but we recommend against using it.
from mongoose.
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.
from mongoose.
For the following code:
if (person.address) {
person.address.zip = 54321;
} else {
person.address = {
zip: 54321;
}
}
The more concise way in Mongoose would be person.set('address.zip', 54321)
. Oddly enough person.address.zip = 54321
also works, because address
is a nested path and nested paths are never undefined on Mongoose documents, but relying on the fact that nested paths are never undefined is not very readable IMO because it isn't obvious why that pattern works without deep understanding of Mongoose.
I agree that person.address = { ...person.address, zip: 54321 }
should work, and we're working on a more complete fix. I'm just trying to emphasize the point that, ideally, you shouldn't use this spread operator pattern for performance reasons: not only do you have an unnecessary shallow clone of a complex object, but you're also telling Mongoose to do a deep diff, and update the entire address
subdocument in MongoDB. Whereas all you want to do is just update the address.zip
property.
from mongoose.
Related Issues (20)
- query.sort(null) should reset the sort options HOT 8
- Mongoose / mongo omitting part of the document when saved HOT 2
- `InferSchemaType` returns `unknown` value HOT 3
- not getting validation error even i am using validate method in my schema HOT 1
- Mongoose v6 modifies connection options object HOT 4
- Model.insertMany with `lean: true` is very slow compared to native driver
- fix: Cannot run `mongoose.createConnection` under `tsx`/ `esbuild`/ `bun` due to the `nextConnectionId` HOT 2
- Populate with TypeScript not working on Cursor HOT 1
- MongoNetworkTimeoutError crashes nodejs process HOT 4
- Missing Typescript detail on Mongoose-specific `options` for updateOne, updateMany, etc. HOT 3
- Facing issue in Mongo+srv which i passing through github actions secrets
- Convenience Method for Converting Array of Strings to ObjectIDs HOT 3
- Round-tripped document fails validation on unmodified Date field HOT 1
- Docs for overwrite option for mongoose.model(), and overwriteModels for mongoose global
- New Mongoose error suddenly due to MongooseError: Buffering timeout HOT 17
- Improve Documentation HOT 1
- BulkSave on entities with virtual properties *and* a transaction: massive performance hit and OOM crashes HOT 5
- Filter query `Condition` type might as well be `any` HOT 3
- pre middleware .save conflict with strict mode HOT 4
- Date coercion and typechecking inconsistent on `bulkWrite` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose.