20.Bulk Write Operations-公式ドキュメント抜粋
15547 ワード
1.順序付けされたアクション・リストは直列に実行されますが、1つの書き込みプロセスで異常なエラーが発生した場合、残りの書き込みは処理されません.
2.無秩序な操作リストは同時実行され、1つの書き込み操作プロセスで異常なエラーが発生した場合、影響を与えずに実行を続行します(同時無秩序)
3.無秩序操作と比較して、秩序操作はスライスの中で一般的に比較的に遅い
4.
5.bulkWriteがサポートするタイプ
6.bulkWriteを使用した操作例
MongoDB provides clients the ability to perform write operations in bulk. Bulk write operations affect a singlecollection. MongoDB allows applications to determine the acceptable level of acknowledgement required for bulk write operations.
New in version 3.2.
The
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 will return without processing any remaining write operations in the list. See ordered Bulk Write
With an unordered list of operations, MongoDB can execute the operations in parallel, but this behavior is not guaranteed. If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list. See Unordered Bulk Write.
Executing an ordered list of operations on a sharded collection will generally be slower than executing an unordered list since with an ordered list, each operation must wait for the previous operation to finish.
By default,
See Execution of Operations
insertOne updateOne updateMany replaceOne deleteOne deleteMany
Each write operation is passed to
For example, the following performs multiple write operations:
The
The following
The operation returns the following:
For more examples, see bulkWrite() Examples
Large bulk insert operations, including initial data inserts or routine data import, can affect sharded clusterperformance. For bulk inserts, consider the following strategies:
If the sharded collection is empty, then the collection has only one initial chunk, which resides on a single shard. 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 in Split Chunks in a Sharded Cluster.
To improve write performance to sharded clusters, use
If your shard key increases monotonically during an insert, then all inserted data goes to the last chunk in the collection, which will always end up on a single shard. Therefore, the insert capacity of the cluster will never exceed the insert capacity of that single shard.
If your insert volume is larger than what a single shard can process, and if you cannot avoid a monotonically increasing shard 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 are no longer monotonically increasing.
SEE ALSO
Shard Keys for information on choosing a sharded key. Also see Shard Key Internals (in particular,Choosing a Shard Key).
2.無秩序な操作リストは同時実行され、1つの書き込み操作プロセスで異常なエラーが発生した場合、影響を与えずに実行を続行します(同時無秩序)
3.無秩序操作と比較して、秩序操作はスライスの中で一般的に比較的に遅い
4.
bulkWrite()
使用5.bulkWriteがサポートするタイプ
insertOne
updateOne
updateMany
replaceOne
deleteOne
deleteMany
6.bulkWriteを使用した操作例
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" },
"replacement" : { "char" : "Tanys", "class" : "oracle", "lvl" : 4 }
}
}
]
);
}
catch (e) {
print(e);
}
Overview
MongoDB provides clients the ability to perform write operations in bulk. Bulk write operations affect a singlecollection. 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 will return without processing any remaining write operations in the list. See ordered Bulk Write
With an unordered list of operations, MongoDB can execute the operations in parallel, but this behavior is not guaranteed. If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list. See Unordered Bulk Write.
Executing an ordered list of operations on a sharded collection will generally be slower than executing an unordered list since with an 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: Each write operation is 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" }, "replacement" : { "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 clusterperformance. For bulk inserts, consider the following strategies:
Pre-Split the Collection
If the sharded collection is empty, then the collection has only one initial chunk, which resides on a single shard. 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 in Split Chunks in a Sharded Cluster.
Unordered Writes to mongos
To improve write performance to sharded clusters, use
bulkWrite()
with the optional parameter ordered
set to false
. mongos
can attempt to send the writes to multiple shards simultaneously. For emptycollections, first pre-split the collection as described in Split Chunks in a Sharded Cluster. Avoid Monotonic Throttling
If your shard key increases monotonically during an insert, then all inserted data goes to the last chunk in the collection, which will always end up on a single shard. Therefore, the insert capacity of the cluster will never exceed the insert capacity of that single shard.
If your insert volume is larger than what a single shard can process, and if you cannot avoid a monotonically increasing shard key, then consider the following modifications to your application:
EXAMPLE
The following example, in C++, swaps the leading and trailing 16-bit word of BSON ObjectIds generated so they are no longer 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 << "name" << "jane" ); // now we may insert o into a sharded collection }
SEE ALSO
Shard Keys for information on choosing a sharded key. Also see Shard Key Internals (in particular,Choosing a Shard Key).