Skip to content

Commit f2a0ce4

Browse files
DOCSP-28958 Clarify Typescript Find and the _id section (#644) (#652)
(cherry picked from commit a498473)
1 parent e78c376 commit f2a0ce4

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
@@ -266,19 +266,28 @@ Find Methods and the _id Field
266266
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
267267

268268
The ``find`` and ``findOne`` methods of the ``Collection`` class include
269-
the ``_id`` field in their return type.
269+
the ``_id`` field in their return type. The driver infers the type of the
270+
returned ``_id`` field based on the type parameter you passed to your
271+
``Collection`` instance.
270272

271273
If the type parameter you passed to your ``Collection`` instance includes the
272-
``_id`` field, the type of the ``_id`` field in the values returned from the
273-
find methods is the same as the type of the ``_id``
274-
field in your type parameter.
274+
``_id`` field in its schema, the driver infers that the ``_id`` field returned
275+
from the method is of the type specified in the schema.
275276

276-
If the type parameter you passed to your ``Collection`` instance does not include the ``_id``
277-
field, the driver gives the ``_id`` field in the values returned from the find methods
278-
the type ``ObjectId``.
277+
However, if the type parameter you passed to your ``Collection`` instance does not
278+
include the ``_id`` field in its schema, the driver infers that the type of the
279+
``_id`` field returned from the method is ``ObjectId``.
279280

280-
The following code uses the :ref:`Pet <mongodb-node-typescript-pet-interface>` interface to return
281-
a document with an ``_id`` of type ``ObjectId``:
281+
.. tip::
282+
283+
The type parameter passed to your ``Collection`` influences only the type
284+
inference of the fields returned from the method. The driver does not convert
285+
the field to the specified type. The type of each field in your type
286+
parameter's schema should match the type of the corresponding field in the
287+
collection.
288+
289+
The following code uses the :ref:`Pet <mongodb-node-typescript-pet-interface>`
290+
interface to return a document with an ``_id`` inferred to be of type ``ObjectId``:
282291

283292
.. code-block:: typescript
284293

@@ -291,7 +300,7 @@ a document with an ``_id`` of type ``ObjectId``:
291300
const id : ObjectId = document._id;
292301

293302
The following code uses the ``IdNumberPet`` interface to return a
294-
document with an ``_id`` field of type ``number``:
303+
document with an ``_id`` inferred to be of type ``number``:
295304

296305
.. code-block:: typescript
297306

0 commit comments

Comments
 (0)