The update query performance varies significantly when the field values are set to the same value. MongoDB version: 4.2.6
Run 1:
no. of documents: 1 million
time taken to update 1M documents: 13m31.227s
db.test.find().forEach(function(doc){db.test.update({_id:doc._id}, {$set:{counter: 400}})})
Run 2:
no. of documents: 1 million
time taken to update 1M documents: 7m41.080s
db.test.find().forEach(function(doc){db.test.update({_id:doc._id}, {$set:{counter: 400}})})
Run 3: After mongod restart and cleaning buff/cache manually
no. of documents: 1 million
time taken to update 1M documents: 7m41.080s
db.test.find().forEach(function(doc){db.test.update({_id:doc._id}, {$set:{counter: 400}})})
Run 4: Setting counter to new value
no. of documents: 1 million
time taken to update 1M documents: 13m44.284s
db.test.find().forEach(function(doc){db.test.update({_id:doc._id}, {$set:{counter: 500}})})
Run 5:
no. of documents: 1 million
time taken to update 1M documents: 7m42.356s
db.test.find().forEach(function(doc){db.test.update({_id:doc._id}, {$set:{counter: 500}})})
Does mongodb perform additional checks while setting the value of a field? What can cause such performance difference for same update operation?