Hemant Rajpoot
Hemant Rajpoot

Reputation: 690

which operation is more expensive in mongodb

I am performing two operations which are following can you tell me which operation is more performant and why? consider there is an index on _id field and isdelete is true already

SomeModel.updateOne({ _id, isDelete: false },{ isDelete: true });

&

SomeModel.updateOne({ _id},{ isDelete: true });

Upvotes: 0

Views: 180

Answers (1)

Dexter
Dexter

Reputation: 982

In your first query, in the filter clause, you are implicitly specifying an AND condition. So optimizer has to scan both '_id' AND 'isDelete' fields

In your second query optimizer will scan only the '_id' index and perform the update operation. If your use-case doesn't need both fields to be validated, 2nd query will be good choice

Upvotes: 1

Related Questions