官术网_书友最值得收藏!

  • 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:

x axis is "inventory orders from/to our warehouse"
> 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]
Diagram with the series of 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().

Since version 3.2 MongoDB has offered an alternative command for bulk writes, BulkWrite().

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.

主站蜘蛛池模板: 马尔康县| 叙永县| 修文县| 区。| 南平市| 桂林市| 岚皋县| 阳西县| 潞西市| 海林市| 南投市| 长宁区| 巨鹿县| 揭阳市| 赫章县| 巫山县| 吴江市| 佳木斯市| 黔西县| 隆尧县| 谢通门县| 牙克石市| 漾濞| 永仁县| 招远市| 宁明县| 嵊州市| 五寨县| 灵台县| 安达市| 中牟县| 什邡市| 道孚县| 澎湖县| 辰溪县| 通海县| 和平区| 大安市| 岢岚县| 北票市| 古交市|