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

[Feature Request] Options to ignore auto conversion id to _id #3184

Open
kbiits opened this issue Oct 22, 2024 · 11 comments
Open

[Feature Request] Options to ignore auto conversion id to _id #3184

kbiits opened this issue Oct 22, 2024 · 11 comments

Comments

@kbiits
Copy link

kbiits commented Oct 22, 2024

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 from merchant.id to merchant._id which results to unexpected query

Describe 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
image

@Abdullahbutt3434
Copy link

I am looking for the solution here

@Abdullahbutt3434
Copy link

**protected $primaryKey = '_id';**
I tried to put above line in model that is not working as well
if we want to go with _id there must a be 
please guide. 

@kbiits
Copy link
Author

kbiits commented Oct 22, 2024

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

@GromNaN
Copy link
Member

GromNaN commented Oct 29, 2024

We have no plan to allow usage of .id fields in embedded documents. For your existing database, you can rename the field with the $rename operator.

DB::collection('products')->raw()->updateMany([], ['$rename' => ['merchant.id' => 'merchant._id']]);

If you need to keep both id and _id fields during update to avoid downtime, you can update in 3 steps:

  1. Duplicate the field merchant.id into merchant._id
  2. Deploy your code updating laravel-mongodb package
  3. Remove merchant.id

@kbiits
Copy link
Author

kbiits commented Oct 29, 2024

That's too scary since we have a lot of services query the same collection and we don't want to break them.
We can still use the v4 version instead of renaming the field. I hope you guys have the bandwidth to support it in the future.

@katjackson
Copy link

I'm also having an issue with this behavior. @GromNaN Would you consider making the aliasIdForQuery method protected, so that it could be overwritten? Renaming the nested id keys in our large code base is just too risky.

@GromNaN
Copy link
Member

GromNaN commented Dec 10, 2024

Could you provide more precision about your data model? Are you using id fields on the main document on only for embedded documents?

@katjackson
Copy link

katjackson commented Dec 10, 2024

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.

{
    "_id" : ObjectId(),
    "dynamic_content" : {
        "conditions" : [
            {
                "id" : "32C67020",
                "name" : "Deposit",
                "description" : "Deposited Students",
                "active" : true,
                "rules" : [
                    {
                        "content" : {...},
                        "type" : "user_segment"
                    }
                ]
            },
            {
                "id" : "6FED4F05",
                "name" : "Admit",
                "description" : "Admitted students-Nondeposits",
                "active" : true,
                "index_weight" : NumberInt(2),
                "rules" : [
                    {
                        "content" : {...},
                        "type" : "user_segment"
                    },
                    {
                        "content" : {...},
                        "type" : "date_condition"
                    }
                ]
            }
        ]
    }
}

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.

aliasIdForResult is public, so I was able to overwrite it, to continue returning '_id' alongside 'id'. But because aliasIdForQuery is private, I would need to overwrite toMql, insert, insertGetId, delete, performUpdate, and inheritConnectionOptions in order to change the behavior of aliasIdForQuery so that I can manage those models with nested 'id' values.

@leitommi
Copy link

leitommi commented Jan 14, 2025

We have no plan to allow usage of .id fields in embedded documents. For your existing database, you can rename the field with the $rename operator.

@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.

@GromNaN
Copy link
Member

GromNaN commented Jan 14, 2025

We plan to add a setting to disable _id conversion for embedded documents.

@leitommi
Copy link

We plan to add a setting to disable _id conversion for embedded documents.

It is superb to hear that! Is there a target version in mind as well or any other indication about timeline?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants