- Mastering MongoDB 3.x
- Alex Giamas
- 454字
- 2021-08-20 10:10:54
Batch operations using the mongo shell
In the case of inserts, we can generally expect that the order of operations doesn't matter.
Bulk, however, can be used with many more operations than just inserts. In the following example, we have a single book with isbn : 101 and the name Mastering MongoDB in a bookOrders collection with the number of available copies to purchase in the available field, with 99 books available for purchase:
> db.bookOrders.find()
{ "_id" : ObjectId("59204793141daf984112dc3c"), "isbn" : 101, "name" : "Mastering MongoDB", "available" : 99 }
With the following series of operations in a single bulk operation we are adding 1 book to the inventory and then ordering 100 books, for a final total of 0 copies available:

> var bulk = db.bookOrders.initializeOrderedBulkOp();
> bulk.find({isbn: 101}).updateOne({$inc: {available : 1}});
> bulk.find({isbn: 101}).updateOne({$inc: {available : -100}});
> bulk.execute();
Because we are using initializeOrderedBulkOp() we can make sure that we are adding one book before ordering 100 so that we are never out of stock. On the contrary, if we were using initializeUnorderedBulkOp() then we wouldn't have such a guarantee and we might end up with the 100-book order coming in before the addition of the new book, resulting in an application error as we don't have that many books to fulfill the order.
When executing through an ordered list of operations, MongoDB will split the operations into batches of 1,000 and group these by operation. For example, if we have 1,002 inserts, then 998 updates, then 1,004 deletes, and finally 5 inserts we will end up with the following:
[1000 inserts]
[2 inserts]
[998 updates]
[1000 deletes]
[4 deletes]
[5 inserts]

This doesn't affect the series of operations, but implicitly means that our operations will leave the database in batches of 1,000. This behavior is not guaranteed to stay in future versions (!).
If we want to inspect the execution of a bulk.execute() command we can issue bulk.getOperations() right after we execute().
BulkWrite arguments are the series of operations we want to execute, WriteConcern (default is again 1), and if these should go in order (they will be ordered by default):
> db.collection.bulkWrite(
[ <operation 1>, <operation 2>, ... ],
{
writeConcern : <document>,
ordered : <boolean>
}
)
Operations are the same ones supported by Bulk:
- insertOne
- updateOne
- updateMany
- deleteOne
- deleteMany
- replaceOne
updateOne, deleteOne, and replaceOne have matching filters; if they match more than one document, they will only operate on the first one. It's important to design these queries so that they don't match more than one documents or else behavior will be undefined.
- Big Data Analytics with Hadoop 3
- Hands-On Machine Learning on Google Cloud Platform
- 計算機應用復習與練習
- Hands-On Linux for Architects
- 讓每張照片都成為佳作的Photoshop后期技法
- Ceph:Designing and Implementing Scalable Storage Systems
- Splunk Operational Intelligence Cookbook
- 內模控制及其應用
- 人工智能技術入門
- C++程序設計基礎(上)
- Mastering OpenStack(Second Edition)
- Machine Learning with Spark(Second Edition)
- JSP通用范例開發金典
- SQL Server 2019 Administrator's Guide
- 超好玩的Python少兒編程