本文共 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/