Skip to content

DOCSP-28958 Clarify Typescript Find and the _id section #644

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
Changes from 1 commit
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
29 changes: 19 additions & 10 deletions source/fundamentals/typescript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,28 @@ Find Methods and the _id Field
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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

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

The following code uses the :ref:`Pet <mongodb-node-typescript-pet-interface>` interface to return
a document with an ``_id`` of type ``ObjectId``:
.. tip::

The type parameter passed to your ``Collection`` influences only the type
inference of the fields returned from the method. The driver does not convert
the field to the specified type. The type of the fields in your type
parameter's schema should match the type of their corresponding fields in the
collection.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: Slight ambiguity

Suggested change
the field to the specified type. The type of the fields in your type
parameter's schema should match the type of their corresponding fields in the
collection.
the field to the specified type. The type of each field in your type
parameter's schema should match the type of the corresponding field in the
collection.


The following code uses the :ref:`Pet <mongodb-node-typescript-pet-interface>`
interface to return a document with an ``_id`` inferred to be of type ``ObjectId``:

.. code-block:: typescript

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

The following code uses the ``IdNumberPet`` interface to return a
document with an ``_id`` field of type ``number``:
document with an ``_id`` inferred to be of type ``number``:

.. code-block:: typescript

Expand Down