MongoDB:批量插入 (Bulk.insert) 与插入多个 (insert([...]))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35112165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:37:59  来源:igfitidea点击:

MongoDB: Bulk insert (Bulk.insert) vs insert multiple (insert([...]))

mongodb

提问by Jiew Meng

Is there any difference between a bulk insertvs inserting multiple documents?

批量插入插入多个文档之间有什么区别吗?

var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { item: "abc123", defaultQty: 100, status: "A", points: 100 } );
bulk.insert( { item: "ijk123", defaultQty: 200, status: "A", points: 200 } );
bulk.insert( { item: "mop123", defaultQty: 0, status: "P", points: 0 } );
bulk.execute();

VS

VS

db.collection.insert(
   <document or array of documents>
)

Is there one thats faster?

有没有更快的?

回答by Alex

@Dummy is correct that bulk operations are generally faster than individual inserts, however, from version 2.6 and above, inserting multiple documents using collection.insertis just syntactic sugar for a BulkWrite. If you set the orderedflag to false, performance should be identical to an unordered bulk insert:

@Dummy 是正确的,批量操作通常比单个插入更快,但是,从 2.6 及更高版本开始,使用插入多个文档collection.insert只是BulkWrite. 如果将该ordered标志设置为 false,则性能应该与无序批量插入相同:

db.collection.insert(<document array>,{ordered:false})

This operation will return a BulkWriteResult, see more details in the documentation.

此操作将返回一个BulkWriteResult,请参阅文档中的更多详细信息。

回答by Teedeez

Yes, there is difference. With bulk ops (always), you only need to go to the database once and you perform the operation on batches of records, then you return to the application. With individual inserts, you will need to go to the database, do the insert, then go back to the application, and you will repeat this process for every single insert operation you tell the database to perform. So individual inserts are very slow compared to the bulk operations. It is like groceries shopping, if you have everything you need to buy listed out, you can buy them all in one trip (bulk insert), but if for each item, you need to drive to the store to get it, and then drive home, then drive back to the store to buy another item, then it is not very efficient (like individual inserts)

是的,有区别。使用批量操作(总是),您只需要访问数据库一次并对批量记录执行操作,然后返回应用程序。对于单独的插入,您将需要转到数据库,执行插入,然后返回到应用程序,并且您将针对您告诉数据库执行的每个插入操作重复此过程。因此,与批量操作相比,单个插入非常慢。就像买杂货一样,如果你把你需要买的东西都列出来,你可以一次买完(批量插入),但如果每件物品都需要开车去商店拿,然后开车回家,然后开车回商店再买一件,那么效率不高(就像单独插入一样)

Note: Even though, when interacting with MongoDB using the shell, you only need to deal with one insertfunction for bulk and individual inserts alike, you should make a distinction between insertOne()and insertMany()operations when using MongoDB driver APIs. And you should never call insertOne()inside a loop (reason: see my groceries shopping analogy above).

注意:尽管在使用 shell 与 MongoDB 交互时,您只需要处理一个insert函数,用于批量插入和单个插入,但在使用 MongoDB 驱动程序 API 时应该区分insertOne()insertMany()操作。并且您永远不应该insertOne()在循环内调用(原因:请参阅上面我的杂货购物类比)。