Skip to content

Commit 9a85074

Browse files
(DOCSP-20130) TS - _id and Inserts (#294)
1 parent 15fdca5 commit 9a85074

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

source/fundamentals/typescript.txt

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,127 @@ section.
142142

143143
.. _node-driver-limitations:
144144

145+
146+
Insert Operations and the _id Field
147+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
148+
149+
How you specify the ``_id`` field in type parameters passed to your
150+
``Collection`` instance affects the behavior
151+
of insert operations. The following table describes how different
152+
``_id`` field specifications affect insert operations:
153+
154+
.. list-table::
155+
:header-rows: 1
156+
:widths: 20 20 20 40
157+
158+
* - ``_id`` field type
159+
- Example Type
160+
- Required on insert
161+
- Behavior on insert
162+
163+
* - | Unspecified
164+
- | Not applicable
165+
- | No
166+
- | The driver creates an
167+
:manual:`ObjectId </reference/method/ObjectId/>`
168+
value for each inserted document.
169+
170+
* - | Specified
171+
- | ``{ _id: number };``
172+
- | Yes
173+
- | If you do not specify a value for the ``_id`` field in an insert operation,
174+
the driver raises an error.
175+
176+
* - | Specified as optional
177+
- | ``{ _id?: number };``
178+
- | No
179+
- | If you do not specify the ``_id`` field in an insert operation,
180+
the driver adds an ``_id`` field value generated by a
181+
**primary key factory**.
182+
| A primary key factory is a class that defines a system for creating
183+
unique identifiers for documents and implements the ``PkFactory``
184+
interface.
185+
| The default primary key factory generates ``_id`` values of type
186+
``ObjectId``.
187+
188+
If you must specify the ``_id`` field as required in the type you define to represent
189+
documents in your collection but you do not want to specify values for the
190+
``_id`` field in insert operations, use the ``OptionalId`` helper type when you
191+
create your collection. The ``OptionalId`` type accepts a type parameter as an
192+
argument and returns that type with an optional ``_id`` field.
193+
194+
The following code snippet defines the ``IdPet`` interface, which
195+
includes a type for the ``_id`` field:
196+
197+
.. code-block:: typescript
198+
199+
interface IdPet {
200+
_id: ObjectId;
201+
name: string;
202+
age: number;
203+
}
204+
205+
The following code uses the preceding interface along with the
206+
``OptionalId`` type to insert a document without specifying a value for the
207+
``_id`` field:
208+
209+
.. code-block:: typescript
210+
211+
const database = client.db("<your database>");
212+
const collection = db.collection<OptionalId<IdPet>>("<your collection>");
213+
214+
collection.insertOne({
215+
name: "Spot",
216+
age: 2
217+
});
218+
219+
.. important:: Data Consistency and Optional _id Types
220+
221+
If you do not specify a primary key factory method that generates
222+
``_id`` values of your desired type, you may experience data inconsistency.
223+
This occurs because the driver's default primary factory method generates
224+
``_id`` values of type ``ObjectId`` regardless of the type you specify for the
225+
``_id`` field in your type parameter.
226+
227+
To show this behavior, assume you apply the following interface to
228+
your ``Collection`` instance:
229+
230+
.. code-block:: typescript
231+
232+
interface I {
233+
_id?: number;
234+
name: string;
235+
}
236+
237+
The following sequence of insert operations inserts one document
238+
with an ``_id`` of type ``number`` and one document with an
239+
``_id`` of type ``ObjectId`` into your collection:
240+
241+
.. code-block:: typescript
242+
243+
collection.insertOne({ _id: 1, name: "Scar" });
244+
collection.insertOne({ name: "Ursula" });
245+
246+
To ensure that your data is consistent, apply a primary key factory to
247+
your ``MongoClient`` instance like this:
248+
249+
.. code-block:: typescript
250+
251+
const client = new MongoClient(uri, {
252+
pkFactory: { createPk: () => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) },
253+
});
254+
255+
To learn more about the ``_id`` field, see
256+
:manual:`The _id Field </core/document/#the-_id-field>` in the MongoDB
257+
manual.
258+
259+
To learn more about the types, interfaces, and classes discussed in this section, see the
260+
following resources:
261+
262+
- `OptionalId <{+api+}/modules.html#OptionalId>`__ API documentation
263+
- `PkFactory <{+api+}/interfaces/PkFactory.html>`__ API documentation
264+
- :github:`ObjectId <mongodb/js-bson/blob/master/src/objectid.ts>` source code
265+
145266
Known Limitations
146267
-----------------
147268

0 commit comments

Comments
 (0)