Bulk insert in MongoDB

Mongodb Provides the Bulk () as an API that is used to perform the multiple insertions of data. To use the Bulk operation we should initialize the Bulk operation for specified collection.

Example:


var SD = db. studentDetails.initializeUnorderedBulkOp ( )

SD.insert(
            {
             Student_name  : "Ga Eul",
             Department       : "Computer Science and Engg",
             Percentage       : "96%",
             Address            : { Street:"Jakarthan Street", City : "Hyderabad", State:"Telangana"},
             Gender              : "FeMale"
            } );

SD.insert(
            {
             Student_name  :  "Lathika",
             Department       :  "information Technology",
             Percentage       :  "82%",
             Address            : { Street:"Gold phase", City : "Hyderabad", State:"Telangana"},
             Gender              : "FeMale"
            } );

SD.execute();



Successful execution of the Records will throw the below details
BulkWriteResult ({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 2,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

In the above Code initializeUnorderedBulkOp operation returns the list of operations builder which maintains the list of operations to perform.
Mongo DB can operate parallel execution as well as in Non-deterministic order.
In a list of operations any of the operation return failure Mongo DB will process the remaining operations.
SD.insert ( ); is the method used to insert one or more number of operations.
SD.execute ( ); method on the created object will execute and responsible for adding the number of records into the Mongo DB.