Overview
MongoDB provides clients the ability to perform write operations in bulk. Bulk write operations affect a single collection. MongoDB allows applications to determine the acceptable level of acknowledgement required for bulk write operations.
New in version 3.2.
The Db.collection.bulkWrite () method provides the ability to perform bulk INSERT, update, and remove operations. MongoDB also supports BULK insert through the db.collection.insertMany ().
Ordered vs Unordered Operations
Bulk write operations can be either ordered or unordered.
With an ordered list of operations, MongoDB executes the operations serially. If An error occurs during the processing of one of the write operations, MongoDB would return without processing any RE Maining write operations in the list. See Ordered Bulk Write
With a unordered list of operations, MongoDB can execute the operations in parallel, it's not behavior Eed. If An error occurs during the processing of one of the write operations, MongoDB would continue to process remaining WR ITE operations in the list. See Unordered Bulk Write.
Executing an ordered list of operations on a sharded collection would generally be slower than executing an unordered list Since with a ordered list, each operation must wait for the previous operation to finish.
By default, Bulkwrite () performs ordered operations. to specify unordered write operations, set Ordered:false in the options document.
See Execution of Operations
Bulkwrite () Methods
bulkwrite () supports the following write operations:
- Insertone
- Updateone
- Updatemany
- Replaceone
- Deleteone
- Deletemany
Each write operation are passed to Bulkwrite () as a document in an array.
For example, the following performs multiple write operations:
The characters collection contains the following documents:
{"_id": 1, "char": "Brisbane", "Class": "Monk", "LVL": 4},{"_id": 2, "char": "Eldon", "Class": "Alchemist", "lvl ": 3},{" _id ": 3," char ":" Meldane "," Class ":" Ranger "," LVL ": 3}
The following Bulkwrite () performs multiple operations on the collection:
try { db.characters.bulkWrite ( [ {insertone: {" document": { "_id": 4, "char": " Dithras "," Class ":" Barbarian "," LVL ": 4 } } , {insertone: {" document ": { " _id ": 5," char ":" Taeln "," Class ":" Fighter "," LVL ": 3}} , {updateone: { " filter ": {"char": "Eldon"}, "Update": {$set: {"status": "Critical Injury"}}}} , {deleteone: {"Filter": {"char": "Brisbane"}} }, {replaceone: {" filter": {"char": "Meldane"}, "Rep Lacement ": {" char ":" Tanys "," Class ":" Oracle "," LVL ": 4} } ] );} catch (e) { print (e);}
The operation returns the following:
{ "acknowledged": true, "Deletedcount": 1, "Insertedcount": 2, "Matchedcount": 2, " Upsertedcount ": 0, " insertedids ": { " 0 ": 4, " 1 ": 5 }, " Upsertedids ": { }}
For more examples, see bulkwrite () examples
Strategies for Bulk inserts to a sharded Collection
Large BULK INSERT operations, including initial data inserts or routine data import, can affect sharded cluster Performance. For bulk inserts, consider the following strategies:
Pre-split the Collection
If the Sharded collection is empty and then the collection have only one initial chunk, which resides on a single Sha Rd. MongoDB must then take time to receive data, create splits, and distribute the split chunks to the available shards. To avoid this performance cost, you can pre-split the collection, as described on split Chunks in a sharded Cluster.
Unordered writes toMONGOs
To improve write performance to sharded clusters, use bulkwrite () with the optional parameter orderedset To false. MONGOs can attempt to send the writes to multiple shards simultaneously. For emptycollections, first pre-split the collection as described on split Chunks in a sharded Cluster.
Avoid monotonic Throttling
If your Shard key increases monotonically during an insert and then all inserted data goes to the last chunk in the Collectio N, which would always have end up to a single shard. Therefore, the insert capacity of the cluster would never exceed the insert capacity of the.
If your insert volume is larger than what a single shard can process, and if you cannot avoid a monotonically increasing s Hard key, then consider the following modifications to your application:
- Reverse the binary bits of the Shard key. This preserves the information and avoids correlating insertion order with increasing sequence of values.
- Swap the first and last 16-bit words to "shuffle" the inserts.
EXAMPLE: The following example, in C + +, swaps the leading and trailing 16-bit word of BSON objectids generated so they is no longe R monotonically increasing.
Using namespace Mongo;oid make_an_id () { OID x = Oid::gen (); Const unsigned char *p = X.getdata (); Swap ((unsigned short&) p[0], (unsigned short&) p[10]); return x;} void foo () { //Create an object bsonobj o = BSON ("_id" << make_an_id () << "x" << 3 << "n Ame "<<" Jane "); Now we could insert O into a sharded collection}
See Also:shard Keys in information on choosing a sharded key. Also See Shard Key internals (in particular, choosing a Shard key).
Mongodb-mongodb CRUD Operations, Bulk Write Operations