What is the difference between bulk insert and array insert in Mongo db operations
I am wondering how different queries are performance dependent. I understand that bulk inserts are done sequentially, while normal / array inserts are also done sequentially.
BULK INSERT
var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { _id: 1, item: "abc123", status: "A", soldQty: 5000 } );
bulk.insert( { _id: 2, item: "abc456", status: "A", soldQty: 150 } );
bulk.insert( { _id: 3, item: "abc789", status: "P", soldQty: 0 } );
bulk.execute( { w: "majority", wtimeout: 5000 } );
Normal INSERT
db.items.insert( { _id: 1, item: "abc123", status: "A", soldQty: 5000 } );
db.items.insert( { _id: 2, item: "abc456", status: "A", soldQty: 150 } );
db.items.insert( { _id: 3, item: "abc789", status: "P", soldQty: 0 } );
Array INSERT
var array_insert = [
{ _id: 1, item: "abc123", status: "A", soldQty: 5000 } ,
{ _id: 2, item: "abc456", status: "A", soldQty: 150 } ,
{ _id: 3, item: "abc789", status: "P", soldQty: 0 }
] ;
db.items.insert( array_insert );
Can anyone please explain what is the difference between the performance metrics or benefits for the above queries?
source to share
Bulk insert is faster because inserts are sent at the same time. Compare it to a normal stream where you send one insert, wait for the server to respond, and only then send the next insert.
In order to insert all things, regardless of the mode (voluminous and regular), the server gets the same amount of work. In normal mode, time is wasted on the client while it waits for the server to respond. Not necessarily because the server is slow. The network can also be slow.
Batch requests are a common way to speed up an application, regardless of the database used. See pipelining in Redis for example .
source to share