Skip to content

DOCSP-49217: disable id alias conversion in embedded docs #3346

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

Merged
merged 5 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/fundamentals/connection/connection-options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This guide covers the following topics:

- :ref:`laravel-connection-auth-options`
- :ref:`laravel-driver-options`
- :ref:`laravel-disable-id-alias`

.. _laravel-connection-auth-options:

Expand Down Expand Up @@ -349,3 +350,40 @@ item, as shown in the following example:

See the `$driverOptions: array <https://www.mongodb.com/docs/php-library/current/reference/method/MongoDBClient__construct/#parameters>`__
section of the {+php-library+} documentation for a list of driver options.

.. _laravel-disable-id-alias:

Disable Use of id Field Name Conversion
---------------------------------------

Starting in {+odm-long+} v5.0, ``id`` is an alias for the ``_id`` field
in MongoDB documents, and the library automatically converts ``id``
to ``_id`` for both top level and embedded fields when querying and
storing data.

When using {+odm-long+} v5.3 or later, you can disable the automatic
conversion of ``id`` to ``_id`` for embedded documents. To do so,
perform either of the following actions:

1. Set the ``rename_embedded_id_field`` setting to ``false`` in your
``config/database.php`` file:

.. code-block:: php
:emphasize-lines: 6

'connections' => [
'mongodb' => [
'dsn' => 'mongodb+srv://mongodb0.example.com/',
'driver' => 'mongodb',
'database' => 'sample_mflix',
'rename_embedded_id_field' => false,
// Other settings
],
],

#. Pass ``false`` to the ``setRenameEmbeddedIdField()`` method in your
application:

.. code-block:: php

DB::connection('mongodb')->setRenameEmbeddedIdField(false);
5 changes: 4 additions & 1 deletion docs/query-builder.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ the value of the ``title`` field is ``"Back to the Future"``:
:start-after: begin query orWhere
:end-before: end query orWhere

.. note::
.. note:: id Alias

You can use the ``id`` alias in your queries to represent the
``_id`` field in MongoDB documents, as shown in the preceding
Expand All @@ -208,6 +208,9 @@ the value of the ``title`` field is ``"Back to the Future"``:
Because of this behavior, you cannot have two separate ``id`` and ``_id``
fields in your documents.

To learn how to disable this behavior for embedded documents, see the
:ref:`laravel-disable-id-alias` section of the Connection Options guide.

.. _laravel-query-builder-logical-and:

Logical AND Example
Expand Down
5 changes: 5 additions & 0 deletions docs/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ This library version introduces the following breaking changes:
method results before hydrating a Model instance. When passing a complex query
filter, use the ``DB::where()`` method instead of ``Model::raw()``.

Starting in v5.3, you can disable automatic conversion of ``id`` to
``_id`` for embedded documents. To learn more, see the
:ref:`laravel-disable-id-alias` section of the Connection Options
guide.

- Removes support for the ``$collection`` property. The following code shows
how to assign a MongoDB collection to a variable in your ``User`` class in
older versions compared to v5.0:
Expand Down
Loading