Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Cập nhật nhiều hàng trong một truy vấn MongoDB duy nhất

Để thực hiện nhiều thao tác cập nhật khác nhau trên nhiều tài liệu trong một lần giao dịch (batch), MongoDB cung cấp initializeUnorderedBulkOp() (Bulk Write Operations). Điều này giảm độ trễ mạng so với gửi nhiều lệnh riêng lẻ.

Tạo dữ liệu mẫu

> db.upDateMultipleRowsDemo.insertOne({"CustomerName":"John","CustomerPurchaseAmount":500});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb06d78f205348bc626") }
> db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Chris","CustomerPurchaseAmount":700});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb26d78f205348bc627") }
> db.upDateMultipleRowsDemo.insertOne({"CustomerName":"David","CustomerPurchaseAmount":50});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb36d78f205348bc628") }
> db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Larry","CustomerPurchaseAmount":1900});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb46d78f205348bc629") }

Xem dữ liệu

> db.upDateMultipleRowsDemo.find().pretty();

Cập nhật hàng loạt với Bulk Operations

> var manyUpdateValue = db.upDateMultipleRowsDemo.initializeUnorderedBulkOp(); 
> manyUpdateValue.find({ _id: ObjectId("5cd6ceb06d78f205348bc626")}).updateOne({$set:{"CustomerName":"Bob" }}); 
> manyUpdateValue.find({ _id: ObjectId("5cd6ceb36d78f205348bc628")}).updateOne({$set:{"CustomerPurchaseAmount":56544444}}); 
> manyUpdateValue.execute(); 
BulkWriteResult({ 
  "writeErrors" : [ ], 
  "writeConcernErrors" : [ ], 
  "nInserted" : 0, 
  "nUpserted" : 0, 
  "nMatched" : 2, 
  "nModified" : 2, 
  "nRemoved" : 0, 
  "upserted" : [ ] 
})

Kết quả

{ "_id" : ObjectId("5cd6ceb06d78f205348bc626"), "CustomerName" : "Bob", "CustomerPurchaseAmount" : 500 }
{ "_id" : ObjectId("5cd6ceb26d78f205348bc627"), "CustomerName" : "Chris", "CustomerPurchaseAmount" : 700 }
{ "_id" : ObjectId("5cd6ceb36d78f205348bc628"), "CustomerName" : "David", "CustomerPurchaseAmount" : 56544444 }
{ "_id" : ObjectId("5cd6ceb46d78f205348bc629"), "CustomerName" : "Larry", "CustomerPurchaseAmount" : 1900 }

Giải thích

  1. initializeUnorderedBulkOp(): Khởi tạo bulk operation không thứ tự (song song)
  2. find(...).updateOne(...): Thêm các operation vào hàng đợi
  3. execute(): Gửi tất cả lên server trong một request
  4. Unordered: Thực hiện song song, tiếp tục nếu có lỗi

Cách hiện đại: bulkWrite() (MongoDB 3.2+)

API mới nhất, hỗ trợ cả driver và shell:

db.upDateMultipleRowsDemo.bulkWrite([
  {updateOne: {
    filter: {_id: ObjectId("5cd6ceb06d78f205348bc626")},
    update: {$set: {CustomerName: "Bob"}}
  }},
  {updateOne: {
    filter: {_id: ObjectId("5cd6ceb36d78f205348bc628")},
    update: {$set: {CustomerPurchaseAmount: 56544444}}
  }}
]);

Loại Bulk Operations

LoạiMethodĐặc điểm
UnorderedinitializeUnorderedBulkOp()Song song, nhanh, tiếp tục khi lỗi
OrderedinitializeOrderedBulkOp()Tuần tự, dừng khi lỗi

Các operation hỗ trợ trong Bulk

  • insertOne
  • updateOne, updateMany, replaceOne
  • deleteOne, deleteMany

Lưu ý

  • Giới hạn 1000 operations mỗi batch (mặc định), có thể cấu hình
  • Kích thước tối đa 16MB cho toàn bộ batch
  • Không phải transaction ACID (trừ MongoDB 4.0+ với replica set)
  • Trong ứng dụng: Dùng driver's bulkWrite() thay vì shell method