Bulk Insert: How to Work With Bulk Insert Operation

One of the features that is particularly useful when inserting large amount of data is bulk inserting. This is an optimized time-saving approach with few drawbacks that will be described later.

Syntax

documentStore.bulkInsert([database]);
Parameters
database string Name of database for which bulk operation should be performed. If null then the database from DocumentStore will be used.
Return Value
BulkInsertOperation Instance of BulkInsertOperation used for interaction.

BulkInsertOperation

Methods

Signature Description
async abort() Aborts the bulk insert operation. Returns a Promise.
async store(entity, [metadata], [callback]) store the entity, identifier will be generated automatically on client-side. Optional, metadata can be provided for the stored entity. Returns a Promise.
async store(entity, id, [metadata], [callback]) store the entity, with id parameter to explicitly declare the entity identifier. Optional, metadata can be provided for the stored entity. Returns a Promise.
async finish() Finish bulk insert and flush everything to the server. Returns a Promise.

Limitations

There are a couple limitations to the API:

  • The bulk insert operation is broken into batches, each batch is treated in its own transaction so the whole operation isn't treated under a single transaction.

Example

Create bulk insert

Here we create a bulk insert operation and inserting a million documents of type Employee

{
    const bulkInsert = documentStore.bulkInsert();
    for (let i = 0; i < 1000000; i++) {
        const employee = new Employee("FirstName #" + i, "LastName #" + i);
        await bulkInsert.store(employee);
    }

    await bulkInsert.finish();
}