博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bulk更新mongodb的脚本
阅读量:5897 次
发布时间:2019-06-19

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

bulk批处理mongodb,比普通的js脚本来的更快一些。

官方网址:https://docs.mongodb.com/manual/reference/method/Bulk/

bulk支持的方法:

Name Description
Adds an insert operation to a list of operations.
Specifies the query condition for an update or a remove operation.
Adds a single document remove operation to a list of operations.
Adds a multiple document remove operation to a list of operations.
Adds a single document replacement operation to a list of operations.
Adds a single document update operation to a list of operations.
Adds a multi update operation to a list of operations.
Specifies upsert: true for an update operation.
Executes a list of operations in bulk.
Returns an array of write operations executed in the Bulk() operations object.
Returns a JSON document that contains the number of operations and batches in the Bulk() operations object.
Returns the  results as a string.

bulk插入示例:

var bulk = db.items.initializeUnorderedBulkOp();bulk.insert( { item: "abc123", defaultQty: 100, status: "A", points: 100 } );bulk.insert( { item: "ijk123", defaultQty: 200, status: "A", points: 200 } );bulk.insert( { item: "mop123", defaultQty: 0, status: "P", points: 0 } );bulk.execute();

 

bulk更新示例:

/** * 批量更新数据库对象 * 1. 按条件批量更新 * 2. 无条件批量更新 *//** 1. 按条件批量更新 **/// step 1: get key-valuevar idArray = [];var valueArray = [];var idx = -1;db.conch_ChargeSchedule.find({
'predictChargeValue':{$exists:false}}).forEach(function(obj){ idx++; idArray[idx]=obj._id; valueArray[idx]=obj.planValue;});// step 2: updatevar bulk = db.conch_ChargeSchedule.initializeUnorderedBulkOp();for(var i=0; i
var bulk = db.items.initializeUnorderedBulkOp();bulk.find( { status: "D" } ).updateOne( { $set: { status: "I", points: "0" } } );bulk.execute();

打印字符串:

var bulk = db.items.initializeOrderedBulkOp();bulk.insert( { item: "abc123", status: "A", defaultQty: 500, points: 5 } );bulk.insert( { item: "ijk123", status: "A", defaultQty: 100, points: 10 } );bulk.find( { status: "D" } ).removeOne();bulk.toString();

移除:

var bulk = db.items.initializeUnorderedBulkOp();bulk.find( { status: "D" } ).remove();bulk.execute();

 

替换:

var bulk = db.items.initializeUnorderedBulkOp();bulk.find( { item: "abc123" } ).replaceOne( { item: "abc123", status: "P", points: 100 } );bulk.execute();

插入并更新:

var bulk = db.items.initializeUnorderedBulkOp();bulk.find( { status: "P", item: null } ).upsert().updateOne(   {     $setOnInsert: { defaultQty: 0, inStock: true },     $currentDate: { lastModified: true },     $set: { points: "0" }   });bulk.execute();

获得历史:

var bulk = db.items.initializeUnorderedBulkOp();for (var i = 1; i <= 1500; i++) {    bulk.insert( { x: i } );}bulk.execute();bulk.getOperations();// 获得操作历史

普通的js脚本更新mongodb库,为单线程阻塞方式,有数据大小限制,数据大了容易断掉。bulk的则不会出现这种状况,效率max

转载地址:http://xaasx.baihongyu.com/

你可能感兴趣的文章
ping 丢包或不通时链路测试说明
查看>>
好好了解一下cookie
查看>>
聚合(根)、实体、值对象精炼思考总结
查看>>
Hibernate从入门到放弃(三)----持久化对象
查看>>
Aop RealProxy 千年遇BUG
查看>>
java解析虾米音乐
查看>>
rails将类常量重构到数据库对应的表中之三
查看>>
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
查看>>
android本地音乐播放器
查看>>
泛函编程(37)-泛函Stream IO:通用的IO处理过程-Free Process
查看>>
mysql 多行合并函数
查看>>
【案例】RAID卡写策略改变引发的问题
查看>>
Spring Test:Spring Test 4 整合 JUnit 4 使用
查看>>
Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting
查看>>
[python知识] 爬虫知识之BeautifulSoup库安装及简单介绍
查看>>
AFNetworking 2.5.x 网络请求的封装
查看>>
Velocity魔法堂系列三:模板与宿主环境通信
查看>>
【信息统计】dbms_stat 的文档
查看>>
使用NSOperation以及NSOperationQueue
查看>>
大家快来玩转盘抽奖游戏(走在网页游戏开发的路上(七))
查看>>