博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
20.Bulk Write Operations-官方文档摘录
阅读量:5045 次
发布时间:2019-06-12

本文共 5901 字,大约阅读时间需要 19 分钟。

1.有序操作列表将会串行执行,但如果在一个写操作过程出现异常错误,则不会处理剩余的任何写操作

2.无序操作列表将会并发执行,如果在一个写操作过程出现异常错误,则不影响,继续执行(并发无序)

3.对比无序操作,有序操作在分片中普遍比较慢

4.使用  有序操作

5.bulkWrite支持的类型

insertOneupdateOneupdateManyreplaceOnedeleteOnedeleteMany

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  method provides the ability to perform bulk insert, update, and remove operations. MongoDB also supports bulk insert through the .

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 

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 .

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,  performs ordered operations. To specify unordered write operations, setordered false in the options document.

See 

bulkWrite() Methods

 supports the following write operations:

Each write operation is passed to  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  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 

Strategies for Bulk Inserts to a Sharded Collection

Large bulk insert operations, including initial data inserts or routine data import, can affect performance. For bulk inserts, consider the following strategies:

Pre-Split the Collection

If the sharded collection is empty, then the collection has only one initial , 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 .

Unordered Writes to mongos

To improve write performance to sharded clusters, use  with the optional parameter orderedset to false.  can attempt to send the writes to multiple shards simultaneously. For emptycollections, first pre-split the collection as described in .

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:

  • 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   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

 for information on choosing a sharded key. Also see  (in particular,).

转载于:https://www.cnblogs.com/olinux/p/7298707.html

你可能感兴趣的文章
关于页面<!DOCTYPE>声明
查看>>
【AS3代码】播放FLV视频流的三步骤!
查看>>
C++标准库vector使用(更新中...)
查看>>
cocos2d-x 2.2.6 之 .xml文件数据读取
查看>>
枚举的使用
查看>>
BZOJ 1531 二进制优化多重背包
查看>>
BZOJ 2324 (有上下界的)费用流
查看>>
python3基础06(随机数的使用)
查看>>
Zookeeper系列(二)特征及应用场景
查看>>
【HTTP】Fiddler(三)- Fiddler命令行和HTTP断点调试
查看>>
Spring Boot使用Druid和监控配置
查看>>
poi 处理空单元格
查看>>
Android 内存泄漏优化总结
查看>>
luogu4849 寻找宝藏 (cdq分治+dp)
查看>>
Spring Cloud微服务笔记(五)Feign
查看>>
C语言键盘按键列表
查看>>
Codeforces Round #374 (Div. 2)
查看>>
oracle数据类型
查看>>
socket
查看>>
Vue中使用key的作用
查看>>