Git Product home page Git Product logo

Comments (5)

a88zach avatar a88zach commented on May 26, 2024 1

@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.

IslandRhythms avatar IslandRhythms commented on May 26, 2024
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.

vkarpov15 avatar vkarpov15 commented on May 26, 2024

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.

SinBirb avatar SinBirb commented on May 26, 2024

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.

vkarpov15 avatar vkarpov15 commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.