-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[Feature Request] Options to ignore auto conversion id to _id #3184
Comments
I am looking for the solution here |
|
What I've done to get rid of the issue is by downgrading the MongoDB driver to version 4.x |
We have no plan to allow usage of DB::collection('products')->raw()->updateMany([], ['$rename' => ['merchant.id' => 'merchant._id']]); If you need to keep both
|
That's too scary since we have a lot of services query the same collection and we don't want to break them. |
I'm also having an issue with this behavior. @GromNaN Would you consider making the |
Could you provide more precision about your data model? Are you using |
They are in embedded documents. In some places, they are structured as EmbedsMany models on a parent model, and in others, they are just arrays of objects stored on a model.
The changes to alias '_id' in results and queries are incredibly problematic for us. We have almost 200 models in our code base. Our API handles about 3.5 million requests a day to ~1000 endpoints. Changing the structure of the data is not a reasonable option for us.
|
@GromNaN Is it known why subfields rename from "id" to "_id" happens in the first place? To be clear, I am talking about subfields in document, not the primary key "_id". Another note, this change should have been in the release notes. Currently there is mentioned only the primary key. All in all, it is problematic for my project as well and at least opt out would be lifesaver. |
We plan to add a setting to disable |
It is superb to hear that! Is there a target version in mind as well or any other indication about timeline? |
Hey, {
"_id": ObjectId('abcd123'),
"key": "value",
"data": [
0: {
"content": "Lorem ipsum",
"another_key": "another_value",
"_id": "a unique ID from another storage",
"id": "another unique ID from another storage"
},
1: {
"content": "Lorem ipsum II",
"another_key": "another_value_ii",
"_id": "a unique ID from another storage II",
"id": "another unique ID from another storage II"
},
...
]
} When I retrieve this document by Eloquent, all '_id' fields are converted into 'id', event those nested '_id' fields having nothing to do with primary-key. So it would be like this: {
"id": ObjectId('abcd123'),
"key": "value",
"data": [
0: {
"content": "Lorem ipsum",
"another_key": "another_value",
"id": "another unique ID from another storage"
},
1: {
"content": "Lorem ipsum II",
"another_key": "another_value_ii",
"id": "another unique ID from another storage II"
},
...
]
} After realizing it, I encountered a worse case which was overriding |
This _id to id auto conversion is so awkward. I understand the intention and that the maintainers want to unify the package with Laravel way, but introducing such a huge change without any backward care is a huge pain! Our frontend, API, and Wehbook listeners expect to receive the _id field, now, after upgrading to version 5.x, they all receive would appreciate having a way to stop this conversion, as the features introduced in version 5 are amazing, and we would love to upgrade to v5 without weeks of work and downtime! |
Thanks all for your feedback. |
@bisht2050 |
@leitommi the issue copies the description of this pull request, and only links a pull request received from an external contributor (now closed) for reference. At the moment, we're favouring an option to disable this automatic conversion for embedded documents. Embedded documents don't necessarily need an ID (but can profit from having one), so we shouldn't mess around with the identifier in embedded documents. However, since we have made the hard BC break for this, the only way away from this is to keep the BC break in 5 (to avoid having to release version 6 with another BC break), but to allow people to opt out from this new behaviour. Essentially, updating to version 5 of the integration requires you to update this. All in all, we're suffering from an old design decision made here. MongoDB always has used Laravel on the other hand has always used There's this example from earlier which I want to address: {
"content": "Lorem ipsum",
"another_key": "another_value",
"_id": "a unique ID from another storage",
"id": "another unique ID from another storage"
} I hope for your sanity that this is just an example designed to highlight the issue, not an actual use case. Having We will however, keep the current behaviour for root documents: when people use |
this admission has honestly given me some peace lol. thank you for addressing this issue. i spent weeks upgrading our system to v5, and ended up overwriting a lot of the new code to avoid these conversions. but if i had been able to opt out of changing nested _id keys, i might have been able to get away with a simpler solution. so i'm sharing it here in case it helps anyone else. You can use eloquent's $appends property to ensure '_id' is set on all of your serialized models. And then extend all of your models from the adjusted base model class.
This way, you don't have to find every place in your code where you expected '_id' to exist on an array. You will still have to deal with the addition of the new 'id' property, and hide it in places where you don't want to expose the primary key. |
Is your feature request related to a problem?
We have a
merchant.id
field in our mongodb document, and after upgrading to mongodb driver 5.1 we encountered issue that mongodb driver modify the where query frommerchant.id
tomerchant._id
which results to unexpected queryDescribe the solution you'd like
It would be good if there's mechanism to whitelisting the fields from auto conversion
.id
to._id
Describe alternatives you've considered
Additional context
This part of code modify all

.id
to._id
The text was updated successfully, but these errors were encountered: