On this page本页内容
MongoDB provides clients the ability to perform write operations in bulk. MongoDB为客户端提供了批量执行写操作的能力。Bulk write operations affect a single collection. 大容量写入操作影响单个集合。MongoDB allows applications to determine the acceptable level of acknowledgement required for bulk write operations.MongoDB允许应用程序确定批量写入操作所需的可接受确认级别。
New in version 3.2.版本3.2中的新功能。
The db.collection.bulkWrite()
method provides the ability to perform bulk insert, update, and remove operations. db.collection.bulkWrite()
方法提供执行大容量插入、更新和删除操作的能力。MongoDB also supports bulk insert through the MongoDB还支持通过db.collection.insertMany()
.db.collection.insertMany()
进行大容量插入。
Bulk write operations can be either ordered or unordered.大容量写入操作可以是有序的,也可以是无序的。
With an ordered list of operations, MongoDB executes the operations serially. 通过有序的操作列表,MongoDB以串行方式执行操作。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. 如果在处理其中一个写入操作期间发生错误,MongoDB将返回,而不处理列表中的任何剩余写入操作。See ordered Bulk Write请参阅有序批量写入
With an unordered list of operations, MongoDB can execute the operations in parallel, but this behavior is not guaranteed. 对于无序的操作列表,MongoDB可以并行执行这些操作,但不能保证这种行为。If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list. 如果在处理其中一个写入操作期间发生错误,MongoDB将继续处理列表中的其余写入操作。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. bulkWrite()
执行有序操作。To specify 要指定unordered
write operations, set ordered : false
in the options document.unordered
写入操作,请在选项文档中设置ordered: false
。
See Execution of Operations请参阅操作的执行
bulkWrite()
方法¶bulkWrite()
supports the following write operations:支持以下写入操作:
Each write operation is passed to 每个写入操作都作为数组中的文档传递给bulkWrite()
as a document in an array.bulkWrite()
。
For example, the following performs multiple write operations:例如,以下操作将执行多个写入操作:
The characters
collection contains the following documents:characters
集合包含以下文档:
The following 以下bulkWrite()
performs multiple operations on the collection:bulkWrite()
对集合执行多个操作:
The operation returns the following:该操作返回以下内容:
For more examples, see bulkWrite() Examples有关更多示例,请参阅bulkWrite()
示例
Large bulk insert operations, including initial data inserts or routine data import, can affect sharded cluster performance. 大批量插入操作(包括初始数据插入或例行数据导入)可能会影响分片集群的性能。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. 然后,MongoDB必须花时间接收数据、创建分割,并将分割块分发到可用的碎片。To avoid this performance cost, you can pre-split the collection, as described in Split Chunks in a Sharded Cluster.为了避免这种性能损失,您可以预拆分集合,如分片集群中的拆分块中所述。
mongos
mongos
¶To improve write performance to sharded clusters, use 要提高分片集群的写入性能,请使用bulkWrite()
with the optional parameter ordered
set to false
. bulkWrite()
,并将可选参数ordered
设置为false
。mongos
can attempt to send the writes to multiple shards simultaneously. mongos
可以尝试同时向多个碎片发送写操作。For empty collections, first pre-split the collection as described in Split Chunks in a Sharded Cluster.对于空集合,首先按照分片集群中的分割块中所述预分割集合。
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.下面的例子,在C++中,交换BSONObjectIds的前导和后置16位字,使它们不再单调增加。
See also参阅
Shard Keys for information on choosing a sharded key. Shard Keys,获取有关选择分片密钥的信息。Also see Shard Key Internals (in particular, Choosing a Shard Key).另请参见切分键内部(特别是选择切分键)。