0

In MongoDB, are there penalties to replace the whole document when only the value of a specific field is changed?

Will it take longer to update the indexes, etc.?

1 Answers1

1

I suspect it will depend on how many fields you are changing. An update operation where you set all fields vs a replace operation will have similar performance characteristics.

However, an update of a single field vs a replace will have considerably larger overhead - in particular the replication (e.g., the oplog entry) will contain the entire document.

In regards to indexes - I don't believe there's any difference between an update and a replace - if you're interested, you can see details of the index like this:

db.collection.stats({ indexDetails: true }).indexDetails[indexName]

on that the cursor object contains details on the number (and size) of operations on an index - running replaces and updates show the same increase when using update and replace.

Zack Newsham
  • 2,810
  • 1
  • 23
  • 43
  • Even if you replace the entire document, the oplog should only contain the changes, and wiredTiger will rewrite the entire page containing the document for either type of operation. – Joe Jan 03 '23 at 04:11
  • That has not been my experience, while no oplog entry is written when no changes are made, if changes are made, the entire document is written – Zack Newsham Jan 03 '23 at 14:55