Skip to content

Commit e424861

Browse files
norareidyrustagir
andauthored
DOCSP-41335: Id field alias (#3042)
Adds information and an example of the ID field alias. --------- Co-authored-by: norareidy <[email protected]> Co-authored-by: rustagir <[email protected]>
1 parent 837078f commit e424861

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

docs/eloquent-models/model-class.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,8 @@ including this trait, you can make the third-party class compatible with
302302
MongoDB.
303303

304304
When you apply the ``DocumentModel`` trait to a model class, you must
305-
declare the following properties in your class:
306-
307-
- ``$primaryKey = '_id'``, because the ``_id`` field uniquely
308-
identifies MongoDB documents
309-
- ``$keyType = 'string'``, because the {+odm-short+} casts MongoDB
310-
``ObjectId`` values to type ``string``
305+
set the ``$keyType`` property to ``'string'`` as the {+odm-short+}
306+
casts MongoDB ``ObjectId`` values to type ``string``.
311307

312308
Extended Class Example
313309
~~~~~~~~~~~~~~~~~~~~~~

docs/includes/query-builder/QueryBuilderTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Query\Builder;
88
use Illuminate\Pagination\AbstractPaginator;
99
use Illuminate\Support\Facades\DB;
10+
use MongoDB\BSON\ObjectId;
1011
use MongoDB\BSON\Regex;
1112
use MongoDB\Laravel\Collection;
1213
use MongoDB\Laravel\Tests\TestCase;
@@ -63,7 +64,7 @@ public function testOrWhere(): void
6364
// begin query orWhere
6465
$result = DB::connection('mongodb')
6566
->table('movies')
66-
->where('year', 1955)
67+
->where('id', new ObjectId('573a1398f29313caabce9682'))
6768
->orWhere('title', 'Back to the Future')
6869
->get();
6970
// end query orWhere

docs/query-builder.txt

+15-2
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,28 @@ Logical OR Example
176176

177177
The following example shows how to chain the ``orWhere()``
178178
query builder method to retrieve documents from the
179-
``movies`` collection that either match the ``year``
180-
value of ``1955`` or match the ``title`` value ``"Back to the Future"``:
179+
``movies`` collection in which the value of the ``_id``
180+
field is ``ObjectId('573a1398f29313caabce9682')`` or
181+
the value of the ``title`` field is ``"Back to the Future"``:
181182

182183
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
183184
:language: php
184185
:dedent:
185186
:start-after: begin query orWhere
186187
:end-before: end query orWhere
187188

189+
.. note::
190+
191+
You can use the ``id`` alias in your queries to represent the
192+
``_id`` field in MongoDB documents, as shown in the preceding
193+
code. When you run a find operation using the query builder, {+odm-short+}
194+
automatically converts between ``id`` and ``_id``. This provides better
195+
compatibility with Laravel, as the framework assumes that each record has a
196+
primary key named ``id`` by default.
197+
198+
Because of this behavior, you cannot have two separate ``id`` and ``_id``
199+
fields in your documents.
200+
188201
.. _laravel-query-builder-logical-and:
189202

190203
Logical AND Example

docs/upgrade.txt

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ This library version introduces the following breaking changes:
7474
- In query results, the library converts BSON ``UTCDateTime`` objects to ``Carbon``
7575
date classes, applying the default timezone.
7676

77+
- ``id`` is an alias for the ``_id`` field in MongoDB documents, and the library
78+
automatically converts between ``id`` and ``_id`` when querying data. Because
79+
of this behavior, you cannot have two separate ``id`` and ``_id`` fields in your
80+
documents.
81+
7782
.. _laravel-breaking-changes-v4.x:
7883

7984
Version 4.x Breaking Changes

0 commit comments

Comments
 (0)