Skip to content

Commit 9d540b6

Browse files
authored
DOCSP-49217: disable id alias conversion in embedded docs (#3346)
* DOCSP-49217: disable id alias conversion in embedded docs * add cross link * typo fix * JT tech review comment * JT tech review comment 2
1 parent c3bab3c commit 9d540b6

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

Diff for: docs/fundamentals/connection/connection-options.txt

+45
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This guide covers the following topics:
3232

3333
- :ref:`laravel-connection-auth-options`
3434
- :ref:`laravel-driver-options`
35+
- :ref:`laravel-disable-id-alias`
3536

3637
.. _laravel-connection-auth-options:
3738

@@ -349,3 +350,47 @@ item, as shown in the following example:
349350

350351
See the `$driverOptions: array <https://www.mongodb.com/docs/php-library/current/reference/method/MongoDBClient__construct/#parameters>`__
351352
section of the {+php-library+} documentation for a list of driver options.
353+
354+
.. _laravel-disable-id-alias:
355+
356+
Disable Use of id Field Name Conversion
357+
---------------------------------------
358+
359+
Starting in {+odm-long+} v5.0, ``id`` is an alias for the ``_id`` field
360+
in MongoDB documents, and the library automatically converts ``id``
361+
to ``_id`` for both top level and embedded fields when querying and
362+
storing data.
363+
364+
When using {+odm-long+} v5.3 or later, you can disable the automatic
365+
conversion of ``id`` to ``_id`` for embedded documents. To do so,
366+
perform either of the following actions:
367+
368+
1. Set the ``rename_embedded_id_field`` setting to ``false`` in your
369+
``config/database.php`` file:
370+
371+
.. code-block:: php
372+
:emphasize-lines: 6
373+
374+
'connections' => [
375+
'mongodb' => [
376+
'dsn' => 'mongodb+srv://mongodb0.example.com/',
377+
'driver' => 'mongodb',
378+
'database' => 'sample_mflix',
379+
'rename_embedded_id_field' => false,
380+
// Other settings
381+
],
382+
],
383+
384+
#. Pass ``false`` to the ``setRenameEmbeddedIdField()`` method in your
385+
application:
386+
387+
.. code-block:: php
388+
389+
DB::connection('mongodb')->setRenameEmbeddedIdField(false);
390+
391+
.. important::
392+
393+
We recommend using this option only to provide backwards
394+
compatibility with existing document schemas. In new projects,
395+
avoid using ``id`` for field names in embedded documents so that
396+
you can maintain {+odm-long+}'s default behavior.

Diff for: docs/query-builder.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ the value of the ``title`` field is ``"Back to the Future"``:
195195
:start-after: begin query orWhere
196196
:end-before: end query orWhere
197197

198-
.. note::
198+
.. note:: id Alias
199199

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

211+
To learn how to disable this behavior for embedded documents, see the
212+
:ref:`laravel-disable-id-alias` section of the Connection Options guide.
213+
211214
.. _laravel-query-builder-logical-and:
212215

213216
Logical AND Example

Diff for: docs/upgrade.txt

+5
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ This library version introduces the following breaking changes:
127127
method results before hydrating a Model instance. When passing a complex query
128128
filter, use the ``DB::where()`` method instead of ``Model::raw()``.
129129

130+
Starting in v5.3, you can disable automatic conversion of ``id`` to
131+
``_id`` for embedded documents. To learn more, see the
132+
:ref:`laravel-disable-id-alias` section of the Connection Options
133+
guide.
134+
130135
- Removes support for the ``$collection`` property. The following code shows
131136
how to assign a MongoDB collection to a variable in your ``User`` class in
132137
older versions compared to v5.0:

0 commit comments

Comments
 (0)