本文共 1200 字,大约阅读时间需要 4 分钟。
在mongodb2.6以后 使用 Bulk()
or the method.
得到bulk
var bulk = db.users.initializeOrderedBulkOp();bulk.insert( { user: "abc123", status: "A", points: 0 } );bulk.insert( { user: "ijk123", status: "A", points: 0 } );bulk.insert( { user: "mop123", status: "P", points: 0 } );bulk.find( { status: "D" } ).remove();bulk.find( { status: "P" } ).update( { $set: { comment: "Pending" } } );bulk.execute();
如果有一个失败,剩余的会终止,执行是顺序执行的
详细:https://docs.mongodb.org/manual/reference/method/db.collection.initializeOrderedBulkOp/#db.collection.initializeOrderedBulkOp
:
https://docs.mongodb.org/manual/reference/method/db.collection.initializeUnorderedBulkOp/#db.collection.initializeUnorderedBulkOp
var bulk = db.users.initializeUnorderedBulkOp();bulk.insert( { user: "abc123", status: "A", points: 0 } );bulk.insert( { user: "ijk123", status: "A", points: 0 } );bulk.insert( { user: "mop123", status: "P", points: 0 } );bulk.execute();
Each group of operations can have at most . If a group exceeds this , MongoDB will divide the group into smaller groups of 1000 or less. For example, if the bulk operations list consists of 2000 insert operations, MongoDB creates 2 groups, each with 1000 operations.
如果操作过多,会切分成1000个操作
转载地址:http://qodli.baihongyu.com/