在網上搜索了檔案和帖子之后,我對 Mongo 一直不清楚一件事。
當您嘗試像下面的示例那樣將檔案批量寫入集合時,您是否有可能獲得一些成功寫入的檔案,但有些卻沒有?
db.products.insertMany( [
{ item: "card", qty: 15 },
{ item: "envelope", qty: 20 },
{ item: "stamps" , qty: 30 }
] );
換句話說,您是否會遇到這樣的情況:您會為卡片和信封專案創建檔案,而不是為郵票專案創建檔案?
我正在努力提高我公司的一些流程的性能,我的團隊中有一些關于批量插入或更新會真正產生什么樣的錯誤場景的爭論,所以如果有人有明確的答案,那就太好了。我知道,除非您明確說明,否則 mongo 查詢通常不是事務性的,但這是一個尚不清楚的領域。
uj5u.com熱心網友回復:
看看這個例子:
db.products.insertMany([
{ _id: 1, item: "card", qty: 15 },
{ _id: 2, item: "envelope", qty: 20 },
{ _id: 1, item: "stamps", qty: 30 }
]);
uncaught exception: BulkWriteError({
"writeErrors" : [
{
"index" : 2,
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: so.products index: _id_ dup key: { _id: 1.0 }",
"op" : {
"_id" : 1,
"item" : "stamps",
"qty" : 30
}
},
],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
}) :
行為應該是顯而易見的。請注意,默認情況下,檔案的插入順序與您的命令中的順序相同,除非您指定選項ordered: false
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474008.html
標籤:mongodb
上一篇:陣列內的MongoDB查詢
