Skip to content

Commit a498473

Browse files
DOCSP-28958 Clarify Typescript Find and the _id section (#644)
1 parent 4d5dced commit a498473

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

source/fundamentals/typescript.txt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,28 @@ Find Methods and the _id Field
288288
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
289289

290290
The ``find`` and ``findOne`` methods of the ``Collection`` class include
291-
the ``_id`` field in their return type.
291+
the ``_id`` field in their return type. The driver infers the type of the
292+
returned ``_id`` field based on the type parameter you passed to your
293+
``Collection`` instance.
292294

293295
If the type parameter you passed to your ``Collection`` instance includes the
294-
``_id`` field, the type of the ``_id`` field in the values returned from the
295-
find methods is the same as the type of the ``_id``
296-
field in your type parameter.
296+
``_id`` field in its schema, the driver infers that the ``_id`` field returned
297+
from the method is of the type specified in the schema.
297298

298-
If the type parameter you passed to your ``Collection`` instance does not include the ``_id``
299-
field, the driver gives the ``_id`` field in the values returned from the find methods
300-
the type ``ObjectId``.
299+
However, if the type parameter you passed to your ``Collection`` instance does not
300+
include the ``_id`` field in its schema, the driver infers that the type of the
301+
``_id`` field returned from the method is ``ObjectId``.
301302

302-
The following code uses the :ref:`Pet <mongodb-node-typescript-pet-interface>` interface to return
303-
a document with an ``_id`` of type ``ObjectId``:
303+
.. tip::
304+
305+
The type parameter passed to your ``Collection`` influences only the type
306+
inference of the fields returned from the method. The driver does not convert
307+
the field to the specified type. The type of each field in your type
308+
parameter's schema should match the type of the corresponding field in the
309+
collection.
310+
311+
The following code uses the :ref:`Pet <mongodb-node-typescript-pet-interface>`
312+
interface to return a document with an ``_id`` inferred to be of type ``ObjectId``:
304313

305314
.. code-block:: typescript
306315

@@ -313,7 +322,7 @@ a document with an ``_id`` of type ``ObjectId``:
313322
const id : ObjectId = document._id;
314323

315324
The following code uses the ``IdNumberPet`` interface to return a
316-
document with an ``_id`` field of type ``number``:
325+
document with an ``_id`` inferred to be of type ``number``:
317326

318327
.. code-block:: typescript
319328

0 commit comments

Comments
 (0)