8

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.

Abdul Rehman Sayed
  • 6,532
  • 7
  • 45
  • 74
  • Possible duplicate of [What's the difference between insert(), insertOne() and insertMany() method?](http://stackoverflow.com/questions/36792649/whats-the-difference-between-insert-insertone-and-insertmany-method) – styvane May 30 '16 at 11:06

4 Answers4

6

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:

Gerald Mücke
  • 10,724
  • 2
  • 50
  • 67
3

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:

  • 3048s with insert_one()
  • 301s with insert_many() with batches of 1,000 rows
  • 291s with insert_many() with batches of 5,000 rows
  • 273s with insert_many() with batches of 10,000 rows
lcfd
  • 1,326
  • 10
  • 12
2

Performance Test

  • 3.7k x 50000 data
  • Tried to consider same condition
  • Meteor driver wraped up Node driver for Fiber to write a code

Node Driver Test

  • Node driver insertMany with async + Promise sync: 10~11 sec
  • Node driver insertMany with async: 13~15 sec
  • Node driver insert with async: 20~23 sec

Meteor Driver Test

  • Meteor driver insert with async: 24~27 sec
  • Meteor driver insert with sync: 43-49 sec

Conclusion

  • insertMany is much faster than insert
  • Promise + await is faster than async
    • Can't explain. Any Idea?
  • Meteor sync function is slow for bulk insert
kakadais
  • 441
  • 1
  • 4
  • 17
1

When performing operation let say on 30 documents it is more efficient to use insert/update with many option as:

  1. there is one call to server
  2. server engine processes work without waiting for new data

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

profesor79
  • 9,213
  • 3
  • 31
  • 52