Skip to content
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 save document after unsetting a property of an object in a Map, then removing that object from the Map #15108

Closed
2 tasks done
Chee7ah opened this issue Dec 16, 2024 · 0 comments · Fixed by #15114
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Chee7ah
Copy link

Chee7ah commented Dec 16, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.9.1

Node.js version

22.8.0

MongoDB server version

8.0.4

Typescript version (if applicable)

No response

Description

If I have an object within aMap and set a property value in it to undefined (to make mongoose unset it in the database), then proceed to remove that object from the Map altogether, mongoose produces an update that triggers a MongoServerError.

With the reproduction code I get from car.getChanges() an update like { '$unset': { 'owners.abc.name': 1, 'owners.abc': 1 } } which then results in a Updating the path 'owners.abc' would create a conflict at 'owners.abc' MongoServerError.

Steps to Reproduce

const CarSchema = new mongoose.Schema({
  owners: {
    type: Map,
    of: {
      name: String,
    },
  },
});
const CarModel = mongoose.model('Car', CarSchema);

const car = await CarModel.create({
  owners: { abc: { name: 'John' } },
});
car.owners.get('abc').name = undefined;
car.owners.delete('abc');
await car.save();

Expected Behavior

I'd expect to get just { '$unset': { 'owners.abc': 1 } } from car.getChanges() and the save() to succeed afterwards.

@vkarpov15 vkarpov15 added this to the 8.9.2 milestone Dec 17, 2024
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Dec 17, 2024
vkarpov15 added a commit that referenced this issue Dec 18, 2024
fix(map): clean modified subpaths when overwriting values in map of subdocs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants