Interface CollectionInsertManyResult<RSchema>

Overview

Represents the result of an insertMany command on a Collection.

Example

try {
const result = await collection.insertMany([
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
]);
console.log(result.insertedIds);
} catch (e) {
if (e instanceof CollectionInsertManyError) {
console.log(e.insertedIds())
console.log(e.errors())
}
}

The _id fields

The type of the _id fields are 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 fields are provided in any 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.insertMany
  • CollectionInsertManyOptions
interface CollectionInsertManyResult<RSchema> {
    insertedCount: number;
    insertedIds: IdOf<RSchema>[];
}

Type Parameters

  • RSchema

Properties

insertedCount: number

The number of documents that were inserted into the collection.

This is always equal to the length of the insertedIds array.

insertedIds: IdOf<RSchema>[]

The ID of the inserted documents. These may have been autogenerated if no _id was present in any of the inserted documents.

See CollectionInsertManyResult for more information about the inserted id.