@@ -36,22 +36,26 @@ All classes that accept a type parameter in the driver have the default type
36
36
[key: string]: any;
37
37
}
38
38
39
- Any object type can extend the ``Document`` interface.
39
+ All object types extend the ``Document`` interface.
40
40
41
41
For more information on object types, see the
42
42
`TypeScript handbook <https://www.typescriptlang.org/docs/handbook/2/objects.html>`__.
43
43
44
- Extend Document
45
- ~~~~~~~~~~~~~~~
44
+ Type Parameters that Extend Document
45
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46
46
47
- The following classes accept any type that extends the ``Document``
48
- interface:
47
+ The following classes accept all types that both extend
48
+ the ``Document`` interface and are not mutually recursive:
49
+
50
+ .. _node-mongodb-type-parameters-extend-document:
49
51
50
52
- `Collection <{+api+}/classes/Collection.html>`__
51
53
- `ChangeStream <{+api+}/classes/ChangeStream.html>`__
52
54
53
55
You can pass a type parameter that extends the ``Document`` interface like this:
54
56
57
+ .. _mongodb-node-typescript-pet-interface:
58
+
55
59
.. literalinclude:: /code-snippets/typescript/extend-document.ts
56
60
:language: typescript
57
61
:linenos:
@@ -67,10 +71,16 @@ You can pass a type parameter that extends the ``Document`` interface like this:
67
71
:start-after: start-no-key
68
72
:end-before: end-no-key
69
73
70
- Any Type
71
- ~~~~~~~~
74
+ To view an example of a mutually recursive type, which is not supported by the
75
+ :ref:`preceding classes <node-mongodb-type-parameters-extend-document>`,
76
+ see the :ref:`<node-driver-limitations-mutual-recursion>` section.
77
+
78
+ Type Parameters of Any Type
79
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
80
73
- The following classes accept any type parameter:
81
+ The following classes accept all type parameters that are not mutually recursive:
82
+
83
+ .. _node-mongodb-type-parameters-any-type:
74
84
75
85
- `FindCursor <{+api+}/classes/FindCursor.html>`__
76
86
- `AggregationCursor <{+api+}/classes/AggregationCursor.html>`__
@@ -79,81 +89,60 @@ You can find a code snippet that shows how to specify a type for the ``FindCurso
79
89
class in the
80
90
:ref:`Find Multiple Documents Usage Example <node-driver-find-usage-example-code-snippet>`.
81
91
82
- Limitations
83
- -----------
84
-
85
- .. _node-driver-typescript-limitations-dot-notation:
86
-
87
- The driver cannot infer the type of values with keys containing **dot
88
- notation**. Dot notation is a property access syntax for navigating BSON objects.
89
- Click on the tabs to see code snippets that highlight this behavior:
90
-
91
- .. tabs::
92
-
93
- .. tab:: Dot Notation
94
- :tabid: dot-notation
95
-
96
- The following code snippet does not raise a type error:
92
+ To view an example of a mutually recursive type, which is not supported by the
93
+ :ref:`preceding classes <node-mongodb-type-parameters-any-type>`,
94
+ see the :ref:`<node-driver-limitations-mutual-recursion>` section.
97
95
98
- .. literalinclude:: /code-snippets/typescript/dot-notation.ts
99
- :language: typescript
100
- :linenos:
101
- :start-after: start-no-error
102
- :end-before: end-no-error
103
96
104
- .. tab:: Nested Objects
105
- :tabid: nested-objects
97
+ Type Safety and Dot Notation
98
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106
99
107
- The following code snippet raises a type error:
100
+ If you specify a query or update with **dot notation**, the {+driver-short+}
101
+ provides type safety if your query or update does not
102
+ :ref:`reference a nested instance of a recursive type <node-driver-recursive-types-dot-notation>`.
103
+ Dot notation is a syntax you can use to navigate nested JSON objects.
108
104
109
- .. literalinclude:: /code-snippets/typescript/dot-notation.ts
110
- :language: typescript
111
- :linenos:
112
- :start-after: start-error
113
- :end-before: end-error
105
+ The following code snippet defines the ``ClassificationPet`` interface,
106
+ which includes a ``classification`` field that enables you to specify the
107
+ genus and color of dogs and cats:
114
108
115
- This is the error:
109
+ .. code-block:: typescript
116
110
117
- .. code-block:: text
111
+ interface ClassificationPet {
112
+ name: string;
113
+ age: number;
114
+ classification: { genus: "Canis" | "Felis"; color: string };
115
+ }
118
116
119
- Type 'string' is not assignable to type 'number'.
117
+ The following code snippet correctly raises a type error when specifying
118
+ the genus of an unsupported animal in a query:
120
119
121
- Despite the lack of type safety, we still recommend that you use dot notation to
122
- access nested fields in query and update documents when you use TypeScript. You
123
- must manually check that your nested field values have your intended type.
120
+ .. code-block:: typescript
124
121
125
- .. note:: Reason To Use Dot Notation
122
+ database
123
+ .collection<ClassificationPet>("<your collection>")
124
+ .find({ "classification.genus": "Sylvilagus" });
126
125
127
- In the MongoDB Query Language, you must match a subdocument exactly
128
- when specifying subdocuments in a query. Dot notation allows you to query
129
- nested fields without matching subdocuments exactly.
126
+ The type error raised by the preceding code snippet is as follows:
130
127
131
- To show this behavior, lets say you have a collection containing
132
- only the following document:
128
+ .. code-block:: none
133
129
134
- .. code-block:: json
130
+ No overload matches this call.
131
+ ...
132
+ Type '"Sylvilagus"' is not assignable to type 'Condition<"Canis" | "Felis">'.
135
133
136
- { field: { s1: "hi", s2: "bye" } }
134
+ To learn more about dot notation, see
135
+ :manual:`Dot Notation </core/document/#dot-notation>`
136
+ in the MongoDB manual.
137
137
138
- The following query returns no results from this collection, as the value of
139
- ``field`` does not exactly match ``{ s1: "hi" }``:
138
+ To learn more about the limitations of dot notation in the
139
+ {+driver-short+}, see the
140
+ :ref:`<node-driver-recursive-types-dot-notation>`
141
+ section.
140
142
141
- .. literalinclude:: /code-snippets/typescript/note-on-dot-notation.ts
142
- :language: typescript
143
- :linenos:
144
- :start-after: start-no-doc
145
- :end-before: end-no-doc
146
-
147
- The following queries both return your document:
148
-
149
- .. literalinclude:: /code-snippets/typescript/note-on-dot-notation.ts
150
- :language: typescript
151
- :linenos:
152
- :start-after: start-doc
153
- :end-before: end-doc
143
+ .. _node-driver-limitations:
154
144
155
- The syntax of the query that does not use dot notation is cumbersome and hard
156
- to understand, and may not be worth the type safety obtained from
157
- avoiding dot notation.
145
+ Limitations of Driver Version {+version+}
146
+ ---------------------------------
158
147
159
- For more information on dot notation, see :manual:`the MongoDB Manual </core/document/#dot-notation>`.
148
+ .. include:: includes/limitations/{+version+}.rst
0 commit comments