Interface CollectionInsertOneResult<RSchema>

Overview

Represents the result of an insertOne command on a Collection.

Example

const result = await collection.insertOne({
name: 'John',
age: 30,
});

console.log(result.insertedId);

The _id field

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.


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.

💡Tip: See SomeId and CollectionDefinition.defaultId for more information, and concrete examples, on this subject.

See

  • Collection.insertOne
  • CollectionInsertOneOptions
interface CollectionInsertOneResult<RSchema> {
    insertedId: IdOf<RSchema>;
}

Type Parameters

  • RSchema

Properties

Properties

insertedId: IdOf<RSchema>

The ID of the inserted document. This may have been autogenerated if no _id was present in the inserted document.

See CollectionInsertOneResult for more information about the inserted id.