The type of the _id field is inferred from the Collection's type, if it is present.
⚠️Warning: It is the user's responsibility to ensure that the ID type accommodates all possible variations—including auto-generated IDs and any user-provided ones.
If the collection is "untyped", or no _id field is present in its type, then it will default to SomeId, which is a union type covering all possible types for a document ID.
You may mitigate this concern on untyped collections by using a type such as { _id: string } & SomeDoc which would allow the collection to remain generally untyped while still statically enforcing the _id type.
💡Tip: See the SomeId type for more information, and concrete examples, on this subject.
By default, if no _id field is provided in the inserted document, it will be automatically generated and set as a string UUID (not an actual UUID type).
You can modify this behavior by changing the CollectionDefinition.defaultId type when creating the collection; this allows it to generate a UUID or ObjectId instead of a string UUID.
Overview
Represents the result of an
insertOne
command on a Collection.Example
The
_id
fieldThe type of the
_id
field is inferred from the Collection's type, if it is present.If the collection is "untyped", or no
_id
field is present in its type, then it will default to SomeId, which is a union type covering all possible types for a document ID.You may mitigate this concern on untyped collections by using a type such as
{ _id: string } & SomeDoc
which would allow the collection to remain generally untyped while still statically enforcing the_id
type.The default ID
By default, if no
_id
field is provided in the inserted document, it will be automatically generated and set as a string UUID (not an actual UUID type).You can modify this behavior by changing the CollectionDefinition.defaultId type when creating the collection; this allows it to generate a UUID or ObjectId instead of a string UUID.
See