try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
2
Quan trọng
Phương pháp Mongosh
Trang này ghi lại một phương thức
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
3. Đây không phải là tài liệu cho các lệnh cơ sở dữ liệu hoặc trình điều khiển dành riêng cho ngôn ngữ, chẳng hạn như Node.js.try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
3 method. This is not the documentation for database commands or language-specific drivers, such as Node.js.Đối với lệnh cơ sở dữ liệu, xem lệnh
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
4.Đối với trình điều khiển API MongoDB, hãy tham khảo tài liệu trình điều khiển MongoDB dành riêng cho ngôn ngữ.MongoDB driver documentation.
Đối với tài liệu Shell Legacy
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
5, hãy tham khảo tài liệu để phát hành máy chủ MongoDB tương ứng:Mongo Shell v4.4
Mongo Shell v4.2
Chèn nhiều tài liệu vào một bộ sưu tập.
Phương thức
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 có cú pháp sau:try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 method has the following syntax:db.collection.insertMany[ |
[ , , ... ], |
{ |
writeConcern: , |
ordered: |
} |
] |
Tham số
Loại hình
Sự mô tả
|
try { | db.products.insertMany[ [ | { item: "card", qty: 15 }, | { item: "envelope", qty: 20 }, | { item: "stamps" , qty: 30 } | ] ]; | } catch [e] { | print [e]; | } |
7
| tài liệu
| Một mảng các tài liệu để chèn vào bộ sưu tập.
|
try { | db.products.insertMany[ [ | { item: "card", qty: 15 }, | { item: "envelope", qty: 20 }, | { item: "stamps" , qty: 30 } | ] ]; | } catch [e] { | print [e]; | } |
8
| tài liệu
|
Một mảng các tài liệu để chèn vào bộ sưu tập. Không bắt buộc. Một tài liệu thể hiện mối quan tâm viết. Bỏ qua để sử dụng mối quan tâm ghi mặc định.
|
try { | db.products.insertMany[ [ | { item: "card", qty: 15 }, | { item: "envelope", qty: 20 }, | { item: "stamps" , qty: 30 } | ] ]; | } catch [e] { | print [e]; | } |
9
| boolean
| Không đặt rõ ràng mối quan tâm ghi cho hoạt động nếu chạy trong một giao dịch. Để sử dụng viết mối quan tâm với các giao dịch, xem giao dịch và viết mối quan tâm.
|
Returns:Không bắt buộc. Boolean chỉ định xem phiên bản { | "acknowledged" : true, | "insertedIds" : [ | ObjectId["562a94d381cb9f1cd6eb0e1a"], | ObjectId["562a94d381cb9f1cd6eb0e1b"], | ObjectId["562a94d381cb9f1cd6eb0e1c"] | ] | } |
0 có thực hiện chèn được đặt hàng hoặc không đặt hàng hay không. Mặc định là { | "acknowledged" : true, | "insertedIds" : [ | ObjectId["562a94d381cb9f1cd6eb0e1a"], | ObjectId["562a94d381cb9f1cd6eb0e1b"], | ObjectId["562a94d381cb9f1cd6eb0e1c"] | ] | } |
1.Một tài liệu chứa: Một boolean { | "acknowledged" : true, | "insertedIds" : [ | ObjectId["562a94d381cb9f1cd6eb0e1a"], | ObjectId["562a94d381cb9f1cd6eb0e1b"], | ObjectId["562a94d381cb9f1cd6eb0e1c"] | ] | } |
2, được đặt thành { | "acknowledged" : true, | "insertedIds" : [ | ObjectId["562a94d381cb9f1cd6eb0e1a"], | ObjectId["562a94d381cb9f1cd6eb0e1b"], | ObjectId["562a94d381cb9f1cd6eb0e1c"] | ] | } |
1 nếu hoạt động chạy với mối quan tâm ghi hoặc { | "acknowledged" : true, | "insertedIds" : [ | ObjectId["562a94d381cb9f1cd6eb0e1a"], | ObjectId["562a94d381cb9f1cd6eb0e1b"], | ObjectId["562a94d381cb9f1cd6eb0e1c"] | ] | } |
4 nếu viết mối quan tâm bị vô hiệu hóa
|
Một mảng
{ |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
5, chứa các giá trị { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 cho mỗi tài liệu được chèn thành côngtry { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 inserts each document in the array into the collection.Đưa ra một mảng các tài liệu,
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 chèn mỗi tài liệu vào mảng vào bộ sưu tập.Theo mặc định tài liệu được chèn theo thứ tự.try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6
Nếu
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
9 được đặt thành sai, các tài liệu được chèn theo định dạng không đặt hàng và có thể được sắp xếp lại bởi { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
0 để tăng hiệu suất. Các ứng dụng không nên phụ thuộc vào thứ tự chèn nếu sử dụng try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 không có thứ tựSố lượng hoạt động trong mỗi nhóm không thể vượt quá giá trị của MaxWriteBatchSize của cơ sở dữ liệu. Kể từ MongoDB 3.6, giá trị này là
try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
1. Giá trị này được hiển thị trong trường try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
2.Giới hạn này ngăn chặn các vấn đề với các thông báo lỗi quá khổ. Nếu một nhóm vượt quá giới hạn này, trình điều khiển máy khách chia nhóm thành các nhóm nhỏ hơn với số lượng nhỏ hơn hoặc bằng giá trị của giới hạn. Ví dụ: với giá trị try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
3 là try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
1, nếu hàng đợi bao gồm các hoạt động try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
5, trình điều khiển sẽ tạo 2 nhóm, mỗi nhóm có các hoạt động try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
1.
Ghi chú
Trình điều khiển chỉ chia nhóm thành các nhóm nhỏ hơn khi sử dụng API cấp cao. Nếu sử dụng trực tiếp db.runcommand [] [ví dụ: khi viết trình điều khiển], MongoDB đã ném lỗi khi cố gắng thực hiện một lô ghi vượt quá giới hạn.
Bắt đầu từ MongoDB 3.6, một khi báo cáo lỗi cho một lô duy nhất phát triển quá lớn, MongoDB đã cắt giảm tất cả các thông báo lỗi còn lại cho chuỗi trống. Hiện tại, bắt đầu khi có ít nhất 2 thông báo lỗi với tổng kích thước lớn hơn
try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
7.Các kích thước và cơ học nhóm là chi tiết hiệu suất nội bộ và có thể thay đổi trong các phiên bản trong tương lai.
Thực hiện danh sách các hoạt động
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
9 trên bộ sưu tập Sharded thường sẽ chậm hơn so với thực hiện danh sách try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
9 Vì với danh sách được đặt hàng, mỗi thao tác phải chờ kết thúc hoạt động trước đó.try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 creates the collection on successful write.Nếu bộ sưu tập không tồn tại, thì
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 sẽ tạo bộ sưu tập về viết thành công.Nếu tài liệu không chỉ định trường _ID, thì
{ |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
0 sẽ thêm trường { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 và gán một { "acknowledged" : true, "insertedIds" : [ 10, 11, 12 ] }
3 duy nhất cho tài liệu. Hầu hết các trình điều khiển tạo ra một ObjectID và chèn trường { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6, nhưng { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
0 sẽ tạo và điền vào { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 nếu trình điều khiển hoặc ứng dụng không.Nếu tài liệu chứa trường { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6, giá trị { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 phải là duy nhất trong bộ sưu tập để tránh lỗi chính trùng lặp. is not compatible with
try { |
db.products.insertMany[ [ |
{ _id: 13, item: "envelopes", qty: 60 }, |
{ _id: 13, item: "stamps", qty: 110 }, |
{ _id: 14, item: "packing tape", qty: 38 } |
] ]; |
} catch [e] { |
print [e]; |
} |
0try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 không tương thích với try { |
db.products.insertMany[ [ |
{ _id: 13, item: "envelopes", qty: 60 }, |
{ _id: 13, item: "stamps", qty: 110 }, |
{ _id: 14, item: "packing tape", qty: 38 } |
] ]; |
} catch [e] { |
print [e]; |
} |
0
Chèn ném ngoại lệ
try { |
db.products.insertMany[ [ |
{ _id: 13, item: "envelopes", qty: 60 }, |
{ _id: 13, item: "stamps", qty: 110 }, |
{ _id: 14, item: "packing tape", qty: 38 } |
] ]; |
} catch [e] { |
print [e]; |
} |
1.Không bao gồm các lỗi viết mối quan tâm, các hoạt động được đặt hàng dừng sau khi có lỗi, trong khi các hoạt động không theo thứ tự tiếp tục xử lý mọi hoạt động ghi còn lại trong hàng đợi.
Viết các lỗi liên quan được hiển thị trong trường try { |
db.products.insertMany[ [ |
{ _id: 13, item: "envelopes", qty: 60 }, |
{ _id: 13, item: "stamps", qty: 110 }, |
{ _id: 14, item: "packing tape", qty: 38 } |
] ]; |
} catch [e] { |
print [e]; |
} |
2, trong khi tất cả các lỗi khác được hiển thị trong trường try { |
db.products.insertMany[ [ |
{ _id: 13, item: "envelopes", qty: 60 }, |
{ _id: 13, item: "stamps", qty: 110 }, |
{ _id: 14, item: "packing tape", qty: 38 } |
] ]; |
} catch [e] { |
print [e]; |
} |
3. Nếu gặp phải lỗi, số lượng hoạt động ghi thành công được hiển thị thay vì danh sách các _ID được chèn. Các hoạt động được đặt hàng hiển thị lỗi duy nhất gặp phải trong khi các hoạt động không theo thứ tự hiển thị từng lỗi trong một mảng. can be used inside multi-document transactions.
Quan trọng
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
2 có thể được sử dụng bên trong các giao dịch đa tài liệu.Để biết các cân nhắc sử dụng giao dịch bổ sung [như giới hạn thời gian chạy và giới hạn kích thước oplog], xem thêm các cân nhắc sản xuất.
Bắt đầu từ MongoDB 4.4, bạn có thể tạo các bộ sưu tập và chỉ mục bên trong giao dịch đa tài liệu nếu giao dịch không phải là giao dịch ghi chéo.not a cross-shard write transaction.
Cụ thể, trong MongoDB 4.4 và lớn hơn, nếu bạn chỉ định chèn vào một bộ sưu tập không tồn tại trong một giao dịch, bộ sưu tập được tạo ngầm.
Trong MongoDB 4.4 trở lên, hoạt động phải được chạy trên một bộ sưu tập hiện có.
Mẹo
Xem thêm:
Không đặt rõ ràng mối quan tâm ghi cho hoạt động nếu chạy trong một giao dịch. Để sử dụng viết mối quan tâm với các giao dịch, xem giao dịch và viết mối quan tâm.
Các ví dụ sau đây chèn tài liệu vào bộ sưu tập
try { |
db.products.insertMany[ [ |
{ _id: 13, item: "envelopes", qty: 60 }, |
{ _id: 13, item: "stamps", qty: 110 }, |
{ _id: 14, item: "packing tape", qty: 38 } |
] ]; |
} catch [e] { |
print [e]; |
} |
5.
Ví dụ sau sử dụng
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
2 để chèn các tài liệu không chứa trường { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6:try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
2 to insert documents that do not contain the { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 field:try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
Hoạt động trả về tài liệu sau:
{ |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
Bởi vì các tài liệu không bao gồm
{ |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6, { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
0 tạo và thêm trường { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 cho mỗi tài liệu và gán cho nó một giá trị { "acknowledged" : true, "insertedIds" : [ 10, 11, 12 ] }
3 duy nhất.Các giá trị
BulkWriteError[{ |
"writeErrors" : [ |
{ |
"index" : 0, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", |
"op" : { |
"_id" : 13, |
"item" : "stamps", |
"qty" : 110 |
} |
} |
], |
"writeConcernErrors" : [ ], |
"nInserted" : 1, |
"nUpserted" : 0, |
"nMatched" : 0, |
"nModified" : 0, |
"nRemoved" : 0, |
"upserted" : [ ] |
}] |
2 dành riêng cho máy và thời gian khi hoạt động được chạy. Như vậy, các giá trị của bạn có thể khác với các giá trị trong ví dụ.
Ví dụ/Hoạt động sau đây sử dụng
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 để chèn các tài liệu bao gồm trường { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6. Giá trị của { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 phải là duy nhất trong bộ sưu tập để tránh lỗi chính trùng lặp.try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 to insert documents that include the { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 field. The value of { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 must be unique within the collection to avoid a duplicate key error.
try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
Hoạt động trả về tài liệu sau:
{ "acknowledged" : true, "insertedIds" : [ 10, 11, 12 ] }
Bởi vì các tài liệu không bao gồm
{ |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6, { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
0 tạo và thêm trường { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 cho mỗi tài liệu và gán cho nó một giá trị { "acknowledged" : true, "insertedIds" : [ 10, 11, 12 ] }
3 duy nhất.try { |
db.products.insertMany[ [ |
{ _id: 13, item: "envelopes", qty: 60 }, |
{ _id: 13, item: "stamps", qty: 110 }, |
{ _id: 14, item: "packing tape", qty: 38 } |
] ]; |
} catch [e] { |
print [e]; |
} |
Các giá trị
BulkWriteError[{ |
"writeErrors" : [ |
{ |
"index" : 0, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", |
"op" : { |
"_id" : 13, |
"item" : "stamps", |
"qty" : 110 |
} |
} |
], |
"writeConcernErrors" : [ ], |
"nInserted" : 1, |
"nUpserted" : 0, |
"nMatched" : 0, |
"nModified" : 0, |
"nRemoved" : 0, |
"upserted" : [ ] |
}] |
2 dành riêng cho máy và thời gian khi hoạt động được chạy. Như vậy, các giá trị của bạn có thể khác với các giá trị trong ví dụ.BulkWriteError[{ |
"writeErrors" : [ |
{ |
"index" : 0, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", |
"op" : { |
"_id" : 13, |
"item" : "stamps", |
"qty" : 110 |
} |
} |
], |
"writeConcernErrors" : [ ], |
"nInserted" : 1, |
"nUpserted" : 0, |
"nMatched" : 0, |
"nModified" : 0, |
"nRemoved" : 0, |
"upserted" : [ ] |
}] |
Ví dụ/Hoạt động sau đây sử dụng
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
6 để chèn các tài liệu bao gồm trường { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6. Giá trị của { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 phải là duy nhất trong bộ sưu tập để tránh lỗi chính trùng lặp.Chèn một giá trị trùng lặp cho bất kỳ khóa nào là một phần của chỉ mục duy nhất, chẳng hạn như
{ |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6, ném một ngoại lệ. Các nỗ lực sau đây để chèn một tài liệu có giá trị { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 đã tồn tại:Vì
BulkWriteError[{ |
"writeErrors" : [ |
{ |
"index" : 0, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", |
"op" : { |
"_id" : 13, |
"item" : "stamps", |
"qty" : 110 |
} |
} |
], |
"writeConcernErrors" : [ ], |
"nInserted" : 1, |
"nUpserted" : 0, |
"nMatched" : 0, |
"nModified" : 0, |
"nRemoved" : 0, |
"upserted" : [ ] |
}] |
8 đã tồn tại, ngoại lệ sau được ném:try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 11, item: "medium box", qty: 30 }, |
{ _id: 12, item: "envelope", qty: 100}, |
{ _id: 13, item: "stamps", qty: 125 }, |
{ _id: 13, item: "tape", qty: 20}, |
{ _id: 14, item: "bubble wrap", qty: 30} |
], { ordered: false } ]; |
} catch [e] { |
print [e]; |
} |
Lưu ý rằng một tài liệu đã được chèn: tài liệu đầu tiên của
BulkWriteError[{ |
"writeErrors" : [ |
{ |
"index" : 0, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", |
"op" : { |
"_id" : 13, |
"item" : "stamps", |
"qty" : 110 |
} |
} |
], |
"writeConcernErrors" : [ ], |
"nInserted" : 1, |
"nUpserted" : 0, |
"nMatched" : 0, |
"nModified" : 0, |
"nRemoved" : 0, |
"upserted" : [ ] |
}] |
8 sẽ chèn thành công, nhưng chèn thứ hai sẽ thất bại. Điều này cũng sẽ ngăn các tài liệu bổ sung còn lại trong hàng đợi được chèn.BulkWriteError[{ |
"writeErrors" : [ |
{ |
"index" : 2, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 11.0 }", |
"op" : { |
"_id" : 11, |
"item" : "medium box", |
"qty" : 30 |
} |
}, |
{ |
"index" : 5, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", |
"op" : { |
"_id" : 13, |
"item" : "tape", |
"qty" : 20 |
} |
} |
], |
"writeConcernErrors" : [ ], |
"nInserted" : 5, |
"nUpserted" : 0, |
"nMatched" : 0, |
"nModified" : 0, |
"nRemoved" : 0, |
"upserted" : [ ] |
}] |
Với
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
9 đến { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
4, hoạt động chèn sẽ tiếp tục với bất kỳ tài liệu còn lại nào.Các nỗ lực sau đây để chèn nhiều tài liệu với trường
{ |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 và try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 11, item: "medium box", qty: 30 }, |
{ _id: 12, item: "envelope", qty: 100}, |
{ _id: 13, item: "stamps", qty: 125 }, |
{ _id: 13, item: "tape", qty: 20}, |
{ _id: 14, item: "bubble wrap", qty: 30} |
], { ordered: false } ]; |
} catch [e] { |
print [e]; |
} |
3. Mảng tài liệu chứa hai tài liệu có trường { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6 trùng lặp.try { |
db.products.insertMany[ |
[ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 12, item: "medium box", qty: 30 } |
], |
{ w: "majority", wtimeout: 100 } |
]; |
} catch [e] { |
print [e]; |
} |
Hoạt động ném ngoại lệ sau:
try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
0Mặc dù tài liệu có
try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 11, item: "medium box", qty: 30 }, |
{ _id: 12, item: "envelope", qty: 100}, |
{ _id: 13, item: "stamps", qty: 125 }, |
{ _id: 13, item: "tape", qty: 20}, |
{ _id: 14, item: "bubble wrap", qty: 30} |
], { ordered: false } ]; |
} catch [e] { |
print [e]; |
} |
5 và try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 11, item: "medium box", qty: 30 }, |
{ _id: 12, item: "envelope", qty: 100}, |
{ _id: 13, item: "stamps", qty: 125 }, |
{ _id: 13, item: "tape", qty: 20}, |
{ _id: 14, item: "bubble wrap", qty: 30} |
], { ordered: false } ]; |
} catch [e] { |
print [e]; |
} |
6 không thể chèn do các giá trị sao chép { |
"acknowledged" : true, |
"insertedIds" : [ |
ObjectId["562a94d381cb9f1cd6eb0e1a"], |
ObjectId["562a94d381cb9f1cd6eb0e1b"], |
ObjectId["562a94d381cb9f1cd6eb0e1c"] |
] |
} |
6, try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 11, item: "medium box", qty: 30 }, |
{ _id: 12, item: "envelope", qty: 100}, |
{ _id: 13, item: "stamps", qty: 125 }, |
{ _id: 13, item: "tape", qty: 20}, |
{ _id: 14, item: "bubble wrap", qty: 30} |
], { ordered: false } ]; |
} catch [e] { |
print [e]; |
} |
8 cho thấy 5 tài liệu còn lại đã được chèn.Đưa ra một bộ bản sao ba thành viên, thao tác sau đây chỉ định
try { |
db.products.insertMany[ [ |
{ _id: 10, item: "large box", qty: 20 }, |
{ _id: 11, item: "small box", qty: 55 }, |
{ _id: 11, item: "medium box", qty: 30 }, |
{ _id: 12, item: "envelope", qty: 100}, |
{ _id: 13, item: "stamps", qty: 125 }, |
{ _id: 13, item: "tape", qty: 20}, |
{ _id: 14, item: "bubble wrap", qty: 30} |
], { ordered: false } ]; |
} catch [e] { |
print [e]; |
} |
9 của BulkWriteError[{ |
"writeErrors" : [ |
{ |
"index" : 2, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 11.0 }", |
"op" : { |
"_id" : 11, |
"item" : "medium box", |
"qty" : 30 |
} |
}, |
{ |
"index" : 5, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", |
"op" : { |
"_id" : 13, |
"item" : "tape", |
"qty" : 20 |
} |
} |
], |
"writeConcernErrors" : [ ], |
"nInserted" : 5, |
"nUpserted" : 0, |
"nMatched" : 0, |
"nModified" : 0, |
"nRemoved" : 0, |
"upserted" : [ ] |
}] |
0 và BulkWriteError[{ |
"writeErrors" : [ |
{ |
"index" : 2, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 11.0 }", |
"op" : { |
"_id" : 11, |
"item" : "medium box", |
"qty" : 30 |
} |
}, |
{ |
"index" : 5, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", |
"op" : { |
"_id" : 13, |
"item" : "tape", |
"qty" : 20 |
} |
} |
], |
"writeConcernErrors" : [ ], |
"nInserted" : 5, |
"nUpserted" : 0, |
"nMatched" : 0, |
"nModified" : 0, |
"nRemoved" : 0, |
"upserted" : [ ] |
}] |
1 của BulkWriteError[{ |
"writeErrors" : [ |
{ |
"index" : 2, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 11.0 }", |
"op" : { |
"_id" : 11, |
"item" : "medium box", |
"qty" : 30 |
} |
}, |
{ |
"index" : 5, |
"code" : 11000, |
"errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 13.0 }", |
"op" : { |
"_id" : 13, |
"item" : "tape", |
"qty" : 20 |
} |
} |
], |
"writeConcernErrors" : [ ], |
"nInserted" : 5, |
"nUpserted" : 0, |
"nMatched" : 0, |
"nModified" : 0, |
"nRemoved" : 0, |
"upserted" : [ ] |
}] |
2:try { |
db.products.insertMany[ [ |
{ item: "card", qty: 15 }, |
{ item: "envelope", qty: 20 }, |
{ item: "stamps" , qty: 30 } |
] ]; |
} catch [e] { |
print [e]; |
} |
1Mẹo
Xem thêm: