Skip to content

Cannot save document after unsetting a property of an object in a Map, then removing that object from the Map #15108

@Chee7ah

Description

@Chee7ah

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions