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 you are inserting document, MongoDB needs to check whether the document with the same ObjectId exists or not. If it exists document cannot be inserted.
Same case applies to Update. It needs to check whether the document exists or not. Otherwise, update cannot be performed. The case where your update query will slow down is when it can't find document based on your ObjectId / indexed field.
Otherwise, performance for inserting / updating document should be the same.
So Insert can be like this //(Fast)
And Update with upsert (ObjectId available) //(Fast)
Or Update with upsert (Without ObjectId) //This is slow
If 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