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:
find()
and update()
so it does 2 things read and writeThus 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?
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)
And Update with upsert (ObjectId available) //(Fast)
Or Update with upsert (Without ObjectId) //This is slow