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

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

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

salos1982 opened this issue Aug 22, 2023 · 7 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
vkarpov15 added a commit that referenced this issue Jun 4, 2024
docs(migrating_to_7): add id setter to Mongoose 7 migration guide
@vkarpov15 vkarpov15 removed this from the 7.6.13 milestone Jun 5, 2024
@mete89
Copy link

mete89 commented Jul 7, 2024

Hey @vkarpov15

The first doc doesn't include [id setter] section. You may want to update it as well.
https://mongoosejs.com/docs/migrating_to_7.html
https://mongoosejs.com/docs/7.x/docs/migrating_to_7.html

Lastly, what is the suggested migration path from version 6 to avoid being affected by the ID setter change? Is there any option introduced to globally disable it in version 7?

@vkarpov15 vkarpov15 reopened this Jul 12, 2024
@vkarpov15 vkarpov15 added this to the 7.8.0 milestone Jul 12, 2024
@vkarpov15
Copy link
Collaborator

@mete89 the idsetter section was added in da126f4, we just need to merge that to master.

Do you rely on the .id property? if not, the easiest way would be to simply disable the id property for all documents by default:

mongoose.set('id', false);

If you rely on the .id property, you can disable Mongoose's default id property and add your own as follows.

mongoose.set('id', false);
mongoose.plugin(schema => {
  schema.virtual('id').get(function() { return this._id?.toString(); });
});

@vkarpov15 vkarpov15 modified the milestones: 7.8.0, 7.6.13 Jul 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.

6 participants