1
+ .. node-fundamentals-typescript:
2
+
1
3
==========
2
4
TypeScript
3
5
==========
4
6
5
- .. default-domain:: mongodb
6
-
7
7
.. contents:: On this page
8
8
:local:
9
9
:backlinks: none
@@ -17,6 +17,10 @@ In this guide, you can learn about the **TypeScript** features and limitations
17
17
of the MongoDB Node.js driver. TypeScript is a strongly typed programming
18
18
language that compiles to JavaScript.
19
19
20
+ The TypeScript compiler offers type checking in real time. Code editors that
21
+ support TypeScript can provide autocomplete suggestions, display documentation
22
+ inline, and identify type-related errors.
23
+
20
24
All TypeScript features of the driver are optional. All valid JavaScript
21
25
code written with the driver is also valid TypeScript code.
22
26
@@ -120,7 +124,7 @@ You can enable type-checking by constructing filters as ``StrictFilter`` or
120
124
``StrictUpdateFilter`` types.
121
125
122
126
.. warning::
123
-
127
+
124
128
The ``StrictFilter`` and ``StrictUpdateFilter`` types are experimental and
125
129
may show type errors in valid queries where there should be none.
126
130
@@ -139,7 +143,7 @@ filter. The {+driver-short+} reports a type error because the value of
139
143
``classification.color`` is a boolean instead of a string.
140
144
141
145
.. code-block:: typescript
142
-
146
+
143
147
const updateFilter: StrictUpdateFilter<ClassificationPet> = { $set: { "classification.color": false } }
144
148
await pets.updateOne({}, updateFilter);
145
149
@@ -162,7 +166,7 @@ each of which includes a ``time`` field:
162
166
name: string;
163
167
mealtimes: Mealtime[];
164
168
}
165
-
169
+
166
170
interface Mealtime{
167
171
time: string;
168
172
amount: number;
@@ -177,7 +181,7 @@ updates the nested ``time`` field of the ``Mealtime`` instance at index
177
181
:emphasize-lines: 5
178
182
179
183
const mealCounter = 1;
180
-
184
+
181
185
await myColl.findOneAndUpdate(
182
186
{ name: "Lassie" },
183
187
{ $set: { [`mealtimes.${mealCounter}.time` as const]: '4:00 PM' } },
@@ -197,9 +201,9 @@ section.
197
201
Working with the _id Field
198
202
--------------------------
199
203
200
- MongoDB does not recommend specifying the ``_id`` as a part of your model.
201
- Omitting the ``_id`` field makes the model more generic and reusable and more accurately
202
- models the data important to an application. The Node driver’s TypeScript integration
204
+ MongoDB does not recommend specifying the ``_id`` as a part of your model.
205
+ Omitting the ``_id`` field makes the model more generic and reusable and more accurately
206
+ models the data important to an application. The Node driver’s TypeScript integration
203
207
takes care of adding the ``_id`` field to the return types for relevant methods.
204
208
205
209
If you need to work with the ``_id`` field in your models, see the below sections for
@@ -225,12 +229,12 @@ of insert operations. The following table describes how different
225
229
* - | Unspecified
226
230
- | Not applicable
227
231
- | No
228
- - | The driver creates an
232
+ - | The driver creates an
229
233
:manual:`ObjectId </reference/method/ObjectId/>`
230
234
value for each inserted document.
231
235
232
236
* - | Specified
233
- - | ``{ _id: number };``
237
+ - | ``{ _id: number };``
234
238
- | Yes
235
239
- | If you do not specify a value for the ``_id`` field in an insert operation,
236
240
the driver raises an error.
@@ -267,7 +271,7 @@ The following code uses the preceding interface along with the
267
271
268
272
const database = client.db("<your database>");
269
273
const collection = db.collection<OptionalId<IdPet>>("<your collection>");
270
-
274
+
271
275
myColl.insertOne({
272
276
name: "Spot",
273
277
age: 2
@@ -315,7 +319,7 @@ interface to return a document with an ``_id`` inferred to be of type ``ObjectId
315
319
316
320
const database = client.db("<your database>");
317
321
const collection = db.collection<Pet>("<your collection>");
318
-
322
+
319
323
const document = await myColl.findOne({
320
324
name: "Spot",
321
325
});
@@ -374,7 +378,7 @@ document with an ``_id`` inferred to be of type ``number``:
374
378
);
375
379
// Compile time error: Property '_id' does not exist on type 'ProjectedDocument'.
376
380
console.log(doc._id.generationTime);
377
-
381
+
378
382
To view a runnable TypeScript example that includes a find method applying a
379
383
projection, see the
380
384
:ref:`Find a Document <node-driver-findone-usage-example-code-snippet>` page.
0 commit comments