-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
In Mongoose 7.4, we made it possible to modify the document's |
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. |
Please consider releasing a major version when doing something like this in the future and making sure it's marked as a breaking change. |
@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. |
Can the Migrating from 6.x to 7.x migration guide be updated to reference this breaking change? |
docs(migrating_to_7): add id setter to Mongoose 7 migration guide
Hey @vkarpov15 The first doc doesn't include [id setter] section. You may want to update it as well. 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? |
@mete89 the idsetter section was added in da126f4, we just need to merge that to master. Do you rely on the mongoose.set('id', false); If you rely on the mongoose.set('id', false);
mongoose.plugin(schema => {
schema.virtual('id').get(function() { return this._id?.toString(); });
}); |
@vkarpov15 My models have an id field to represent ids in another context so I don't need any virtual here. I set them while creating my objects. So how can I migrate to 7 by disabling this unwanted feature?
|
I upgraded directly to 8.x.x to prevent this issue. |
@mete89 |
Prerequisites
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
if I try to insert object
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
The text was updated successfully, but these errors were encountered: