Reputation: 476
Which query is faster in MongoDB insert query or update query and why that query was faster.
db.collection.insert(
<document or array of documents>
)
or
db.collection.update(
<query>,
<update>
)
Upvotes: 0
Views: 3901
Reputation: 5466
It depends on the document which we are updating or inserting
Insert:
Need to check whether the document of same _id is already exists
if document with same _id exists
then insert failed
else
insert document
Update(with Upsert):
if document with same _id exists
update document
else
insert new document
Insert or Update will be taking same time when we have the document _id provided on the query and other factors such as Indexes we have in the collection also have considerable impact on the query write performance. If a document _id is not provided in insert or update query then it will be slower
You can have a look on these references if you need to improve the performance of your mongodb
https://www.sitepoint.com/7-simple-speed-solutions-mongodb/ https://docs.mongodb.com/manual/core/write-performance/
Upvotes: 2