TypeScript — Node.js
If you use TypeScript, you can specify a type for some classes in the driver.
All classes that accept a type parameter in the driver have the default type
Document
. The Document
interface has the following definition:
All object types extend the Document
interface.
For more information on object types, see the
TypeScript handbook .
Keys not listed in your specified type parameter receive the any type. The following code snippet demonstrates this behavior:
You can pass a type parameter that extends the Document interface like this:
The following classes accept all types that extend the Document interface:
You can find a code snippet that shows how to specify a type for the FindCursor class in the Find Multiple Documents Usage Example .
Type Safety and Dot Notation
Starting in version 5.0, by default, the Node.js driver does not provide type
safety for operations that search on fields expressed in dot notation. Dot
notation is a syntax you can use to navigate nested JSON objects. When
you construct a filter to pass to a query, the driver will not raise a
type error even if you specify an incorrectly typed value for a field expressed
in dot notation.
The following code snippet defines the ClassificationPet
interface,
which includes a classification
field that enables you to specify the
genus and color of dogs and cats:
interface
ClassificationPet
{ name
: string
; age
: number
; classification
: { genus
: "Canis"
| "Felis"
; color
: string
} ;}
The driver does not raise a type error for the following code sample,
even though the value of classification.color
is a boolean
instead of a string:
await
myColl.findOneAndDelete
( { "classification.color"
: false
}) ;
You can enable type-checking by constructing filters as StrictFilter
or
StrictUpdateFilter
types.
Warning
The StrictFilter
and StrictUpdateFilter
types are experimental and
may show type errors in valid queries where there should be none.
In the following code sample, the filter is assigned a
StrictFilter
type. Given this filter type, the Node.js driver
reports a type error because the value of classification.color
is a
boolean instead of a string.
const
filterPredicate
: StrictFilter
<ClassificationPet
> = { "classification.color"
: false
} ;await
myColl.findOneAndDelete
( filterPredicate) ;
The following example assigns a StrictUpdateFilter
type to an update
filter. The Node.js driver reports a type error because the value of
classification.color
is a boolean instead of a string.
const
updateFilter
: StrictUpdateFilter
<ClassificationPet
> = { $set
: { "classification.color"
: false
} }await
pets.updateOne
( { } , updateFilter) ;
Referencing Keys that Incorporate Variables
If you need to query a collection or perform another operation with a
key that incorporates variables, you must use an as const
assertion when specifying the key. This mechanism allows your
code to compile successfully as long as the input types are correct.
The following code snippet defines the ClassificationPet
interface
and the Mealtime
interface. ClassificationPet
includes a
mealtimes
field that contains an array of Mealtime
interfaces,
each of which includes a time
field:
interface
ClassificationPet
{ name
: string
; mealtimes
: Mealtime
[ ] ;}interface
Mealtime
{ time
: string
; amount
: number
;}
The following code snippet performs a find-and-update operation on a
collection of ClassificationPet
documents. The operation
updates the nested time
field of the Mealtime
instance at index
1
. The index position is specified by the variable mealCounter
:
const
mealCounter = 1
;await
myColl.findOneAndUpdate
( { name
: "Lassie"
} , { $set
: { [`mealtimes.
${mealCounter}
.time`
as
const
] : '4:00 PM'
} } ,) ;
To learn more about dot notation, see
Dot Notation
in the MongoDB manual.
To learn more about the limitations of dot notation in the
Node.js driver, see the
Recursive Types and Dot Notation
section.