Abdul Rehman Sayed Abdul Rehman Sayed - 2 years ago 147
Node.js Question

Performance implications of db.collection.insert vs db.collection.insertOne & db.collection.insertMany

I am using mongodb driver for NodeJs, there are 3 methods in it:

1) db.collection.insert

2) db.collection.insertOne

3) db.collection.insertMany

I find that db.collection.insert does the job of both insertOne as well as insertMany.

I also find the same methods for delete & Updates.

Are there any performance impact in calling the db.collection.insert method vis-a-vis the db.collection.insertOne & db.collection.insertMany methods ?

It is safe to assume that I am working in a collection which will have million records at some point in time.

Answer Source

There are some minor differences:

So it depends on your access pattern which to choose. In general, for performance, if you have to add multiple documents, try to make 1 call only (insert() or insertMany()) and have it unordered (if possible from application point of view). If it's a question on whether you should use (insert() or insertMany()) it depends on if you need explain() or the resulting objectIds, but there are no differencese regarding performance (if you type: db.yourCollection.insert or db.yourCollection.insertMany (without ()) you'll see that both perform a bulk.insert(obj))


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download