MongoDB Bulk Insert - MongoDB insertMany With Examples

We will look into MongoDB bulk insert today. Multiple documents can be inserted at a time in MongoDB using bulk insert operation where an array of documents is passed to the insert method as parameter.

MongoDB bulk insert

MongoDB bulk insert performs ordered insert by default. If an error occurs during the insertion at a certain point, the insertion does not happen for the remaining documents.

Lets see an example of how to insert multiple documents using mongodb bulk insert through command line.

MongoDB insert many documents

This operation inserted five documents. MongoDB creates an id field automatically if not specified by the user in the query. The “nInserted” column tells the user number of documents that are inserted.

To view the inserted documents perform the following query as shown below.

Read more about MongoDB find and MongoDB insert operations.

While inserting it is not mandatory for user to provide all the fields in the query. Now lets see how the insert works when some of the fields are not specified.

MongoDB Bulk Insert documents specifying some of the fields

In this example, for the second document, the id field is not specified by the user and for the third document only id, name and speed fields are supplied in the query. The query does a successful insertion even though some fields are missing in the second and third documents. The nInserted column says that three documents were inserted.

Invoke find method and check the inserted documents.

Note that the id is automatically generated by MongoDB for the car “Santro”. For id 8 – only name and speed fields are inserted.

Inserting an unordered documents

While performing unordered insertion, if an error occurs at a certain point the mongodb continues to insert the remaining documents in an array.

For example;

The ordered false is specified in the insert query indicating that it is an unordered collection.


The documents are inserted and as you can see, it is an unordered insert.
If the insert method encounters an error, the result includes the “WriteResult.writeErrors” field indicating the error message that caused failure.

Inserting duplicate id value

The error indicates that we are inserting a document for id 6 which already contains a document and hence throws duplicate key error for id of value 6.

MongoDB Bulk.insert() method

This method performs an insert operation in bulk numbers. It is introduced from version 2.6 onwards.
The syntax is Bulk.insert(<document>).

document: specifies the document to be inserted.

Now we shall see the example for bulk insertion.

Bulk Unordered insert

An unordered list named carbulk is created and insert query is specified with the fields, values to be inserted. Note that it is necessary to call the execute() method following the last insert statement to ensure that the data is actually inserted into the database.

MongoDB Bulk Ordered Insert

This is similar to unordered bulk insert but we use initializeOrderedBulkOp call.

First we create an ordered list of car collection named carbulk1 and then insert the documents by invoking the execute() method.

MongoDB Bulk Insert Java Program

Let’s see a java program for different bulk operations, that we have seen using shell commands till now. Below is the java program for bulk insert using MongoDB java driver version 2.x.

Below is the output of above program.

If you are using MongoDB java driver 3.x, then use below program.

Below image shows sample run of above mongodb bulk insert java program.


That’s all for bulk insert in MongoDB using Mongo shell and java driver, we will look into more MongoDB operations in coming posts.

By admin

Leave a Reply

%d bloggers like this: