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.
If you have multiple documents to insert, insertMany is better, faster & recommended.
With insertOne you can insert one document into the collection. insertMany accepts an array of documents and these are inserted. The insert (the method from older versions) takes a single document by default, and there is an option to insert multiple documents supplied as an array.
The insertMany() method returns a document that contains: The acknowledged key sets to true if operation executed with a write concern or false if the write concern was disabled. An array of _id values of successfully inserted documents.
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)
)
TL;DR:
When performing operation let say on 30 documents it is more efficient to use insert/update with many option as:
so reducing roundtrips, network overhead and allows db engine to process all documents at once give listed advantages over looping via all docs on client side calling insert/update one by one
It was a 10-fold difference performance-wise bw insert_one()
and insert_many()
in my tests.
I am using Python 3.9 and MongoDB 5.0, I am inserting each line of a CSV file of ~5M lines/~500MB as one document into a MongoDb collection. It resulted in a 4.8GB collection, each document about 1KB as reported by MongoDB Compass.
It took:
insert_one()
insert_many()
with batches of 1,000 rowsinsert_many()
with batches of 5,000 rowsinsert_many()
with batches of 10,000 rowsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With