MongoDB: Update/Upsert vs Insert

Jiew Meng picture Jiew Meng · Jan 31, 2016 · Viewed 26.3k times · Source

Recently I notice a huge performance difference between doing multiple upserts (via bulk operations) vs an insert (multiple documents). I would like to know if I am correctly on this:

  • Upsert/Updates will be like a find() and update() so it does 2 things read and write
  • Insert will just write so its a lot faster

Thus the performance difference?

If this is the case, I wonder if I need a lot of writes regularly, instead of updating a document, I write a new document with a createdOn field. Then to query, I will just query for documents, sorted by createdOn DESC. I wonder if this is a good method? Or is there a better way?

  • I do wonder if I have index on the collection, might it speed up the update? But wont this index slow down the write portion then?
  • With the 2nd way, where I only do inserts, will it slow down then I have too many documents? Is it practical (to speed up the writes)?
  • I have also tried increasing the connection pool size. Not sure whats the optimum, but I tried 20 and I see I can handle abt 20 queries per sec thru mongostat. I expected it to be alot higher.

Answer

Code OverFlow picture Code OverFlow · Jan 16, 2017

If your inserting document, Mongodb needs to check whether the document with the same objectId is exists or not. If its exists document cannot be inserted.

Same case apply to Update. It needs to check whether the document exists or not. else update cannot be performed. The case where your update query will slow if your not finding document based on your ObjectId / Indexed field.

Else performance for inserting / updating document should be same.

Eg.....

So Insert can be like this //(Fast)

  1. (Check for document -> Not Found -> Insert new document) Else
  2. (Check for document -> Found -> Cannot Inserted)

And Update with upsert (ObjectId available) //(Fast)

  1. (Check for document -> Not Found -> Insert new document) Else
  2. (Check for document -> Found -> Update the document)

Or Update with upsert (Without ObjectId) //This is slow

  1. (Find ObjectId's (Slow) -> Not Found -> Insert new document) Else
  2. (Find ObjectId's (Slow)-> Found -> Update the documents)