Skip to content

Commit 9aaf673

Browse files
(DOCSP-20130) TS - _id and Find Methods (#295)
1 parent 9a85074 commit 9aaf673

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

source/fundamentals/crud/read-operations/project.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Specify Which Fields to Return
1010
:depth: 2
1111
:class: singlecol
1212

13+
14+
.. _mongodb-node-projection:
15+
1316
Overview
1417
--------
1518

source/fundamentals/typescript.txt

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ section.
142142

143143
.. _node-driver-limitations:
144144

145-
146145
Insert Operations and the _id Field
147146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
148147

@@ -263,6 +262,100 @@ following resources:
263262
- `PkFactory <{+api+}/interfaces/PkFactory.html>`__ API documentation
264263
- :github:`ObjectId <mongodb/js-bson/blob/master/src/objectid.ts>` source code
265264

265+
Find Methods and the _id Field
266+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
267+
268+
The ``find`` and ``findOne`` methods of the ``Collection`` class include
269+
the ``_id`` field in their return type.
270+
271+
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.
275+
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``.
279+
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``:
282+
283+
.. code-block:: typescript
284+
285+
const database = client.db("<your database>");
286+
const collection = db.collection<Pet>("<your collection>");
287+
288+
const document = await collection.findOne({
289+
name: "Spot",
290+
});
291+
const id : ObjectId = document._id;
292+
293+
The following code uses the ``IdNumberPet`` interface to return a
294+
document with an ``_id`` field of type ``number``:
295+
296+
.. code-block:: typescript
297+
298+
interface IdNumberPet {
299+
_id: number;
300+
name: string;
301+
age: number;
302+
}
303+
304+
const database = client.db("<your database>");
305+
const collection = db.collection<IdNumberPet>("<your collection>");
306+
307+
const document = await collection.findOne({
308+
name: "Spot",
309+
});
310+
const id : number = document._id;
311+
312+
.. important:: Projection
313+
314+
If you specify a :ref:`projection <mongodb-node-projection>` in a find
315+
method, you should pass a type parameter to your find method that reflects
316+
the structure of your projected documents.
317+
Without a type parameter, TypeScript cannot check at compile time that you
318+
are using your projected documents safely.
319+
320+
To show this behavior, the following code snippet passes type checking but
321+
raises an error at runtime:
322+
323+
.. code-block:: typescript
324+
325+
const doc = await collection.findOne(
326+
{},
327+
{ projection: { _id: 0, name: 1 } }
328+
);
329+
console.log(doc._id.generationTime);
330+
331+
To catch this error at compile time, pass a type parameter that does not include
332+
the ``_id`` field to your find method:
333+
334+
.. code-block:: typescript
335+
336+
interface ProjectedDocument {
337+
name: string
338+
}
339+
340+
const doc = await collection.findOne<ProjectedDocument>(
341+
{},
342+
{ projection: { _id: 0, name: 1 } }
343+
);
344+
// Compile time error: Property '_id' does not exist on type 'ProjectedDocument'.
345+
console.log(doc._id.generationTime);
346+
347+
To view a runnable TypeScript example that includes a find method applying a
348+
projection, see the
349+
:ref:`Find a Document <node-driver-findone-usage-example-code-snippet>` page.
350+
351+
352+
To learn more about the classes and methods discussed in this section, see the following
353+
API documentation:
354+
355+
- `Collection <{+api+}/classes/Collection.html>`__
356+
- `find <{+api+}/classes/Collection.html#find>`__
357+
- `findOne <{+api+}/classes/Collection.html#findOne>`__
358+
266359
Known Limitations
267360
-----------------
268361

0 commit comments

Comments
 (0)