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

_id and id in document are treated the same since 7.4.0 #13762

Open
2 tasks done
salos1982 opened this issue Aug 22, 2023 · 5 comments · Fixed by #13854
Open
2 tasks done

_id and id in document are treated the same since 7.4.0 #13762

salos1982 opened this issue Aug 22, 2023 · 5 comments · Fixed by #13854
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@salos1982
Copy link

Prerequisites

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

Mongoose version

7.4.0

Node.js version

18.x

MongoDB server version

7.0

Typescript version (if applicable)

5.0

Description

When I try to insert object that contains id field it is transformed to _id field and then is trying to insert.
Deleting id field before insert helps.

Steps to Reproduce

I have schema without id field. But when I try to insert new object with id field it is treated as string version of _id.
E.g. Schema

export class A {
name: string;
}

if I try to insert object

{
  id: "existing _id of another object"
  name: "A"
}

I got E11000 duplicate key error collection
Mongoose 7.3.3 has no such error

Expected Behavior

New object in inserted without error and id field will be ignored

@vkarpov15
Copy link
Collaborator

In Mongoose 7.4, we made it possible to modify the document's _id property via id. doc.id = 42 is now equivalent to doc._id = 42;. However, I can see this causing trouble if you use new MyModel(obj) and obj contains both id and _id properties. We'll fix it so that new MyModel(obj) ignores id if _id is set.

@vkarpov15 vkarpov15 added this to the 7.4.5 milestone Aug 22, 2023
@salos1982
Copy link
Author

I would prefer to have option to disable such functionality. I have a lot of code where I get objects with id field and then insert them to database. And very often there are ids of previous objects and it causes duplicate key errors.

@zeljkocurcic
Copy link

Please consider releasing a major version when doing something like this in the future and making sure it's marked as a breaking change.

@vkarpov15
Copy link
Collaborator

@zeljkocurcic we didn't see this as a breaking change when we released it, but in hindsight we definitely should have. We removed this id setter in Mongoose 8.

@tonynelson19
Copy link

Can the Migrating from 6.x to 7.x migration guide be updated to reference this breaking change?

@vkarpov15 vkarpov15 reopened this May 20, 2024
@vkarpov15 vkarpov15 modified the milestones: 7.5.2, 7.6.12 May 20, 2024
@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label May 20, 2024
@vkarpov15 vkarpov15 modified the milestones: 7.6.12, 7.6.13 May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants