bulk更新mongodb的脚本
bulk批处理mongodb,比普通的js脚本来的更快一些。
官方网址:https://docs.mongodb.com/manual/reference/method/Bulk/
bulk支持的方法:
Name | Description |
---|---|
Bulk.insert() |
Adds an insert operation to a list of operations. |
Bulk.find() |
Specifies the query condition for an update or a remove operation. |
Bulk.find.removeOne() |
Adds a single document remove operation to a list of operations. |
Bulk.find.remove() |
Adds a multiple document remove operation to a list of operations. |
Bulk.find.replaceOne() |
Adds a single document replacement operation to a list of operations. |
Bulk.find.updateOne() |
Adds a single document update operation to a list of operations. |
Bulk.find.update() |
Adds a multi update operation to a list of operations. |
Bulk.find.upsert() |
Specifies upsert: true for an update operation. |
Bulk.execute() |
Executes a list of operations in bulk. |
Bulk.getOperations() |
Returns an array of write operations executed in the Bulk() operations object. |
Bulk.tojson() |
Returns a JSON document that contains the number of operations and batches in the Bulk() operations object. |
Bulk.toString() |
Returns the Bulk.tojson() 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-value var 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: update var bulk = db.conch_ChargeSchedule.initializeUnorderedBulkOp(); for(var i=0; i<idArray.length; i++){ bulk.find( { _id: idArray[i] } ).update({ $set: { predictChargeValue: valueArray[i] } }); } bulk.execute(); /** 2. 无条件批量更新 **/ var bulk = db.conch_ChargeSchedule.initializeUnorderedBulkOp(); bulk.find({}).update({ $set: { isPlanValueUpdatable: true, isStatusUpdatable:true, isStartDateUpdatable:true } }); bulk.execute();
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