Skip to content

Query by "id" field problem #3380

Closed as not planned
Closed as not planned
@LNDev

Description

@LNDev
  • Laravel-mongodb Version: 5.4.1
  • PHP Version: 8.2.8
  • Database Driver & Version:
    MongoDb 1.21.0

Description:

The issue arises when queries are performed using the "id" field on collections where the values of _"id" and "id" are different. In such cases, querying by "id" returns an empty result.

For example:

ExampleModel::where('id', $id);

returns an empty result.

I think this happens due to three main reasons:

First, the method getRenameEmbeddedIdField() returns true by default if the rename_embedded_id_field setting is not defined in the config/database.php file.

Second, in the methods "aliasIdForQuery" and "aliasIdForResult", if rename_embedded_id_field is not defined or is explicitly set to true or explicitly set to false (in any way, then), the "id" field is always internally converted to "_id" (in root). As a result, any query targeting "id" actually ends up querying "_id".

Third, during the compilation of the "where" clause by the "compileWheres" method, the "id" column is again transformed into "_id". This means that any condition written on "id" is effectively applied to "_id" instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions