Title: Does updating a MongoDB record rewrites the whole record or only the updated fields?
Tags: mongodb
Question: I have a MongoDB collection as follows:

```comment_id (number)
comment_title (text)
score (number)
time_score (number)
final_score (number) 
created_time (timestamp)
```

Score is and integer that's usually updated using $inc 1 or -1 whenever someone votes up or down for that record.
but time_score is updated using a function relative to timestamp and current time and other factors like how many (whole days passed) and how many (whole weeks passed) .....etc

So I do $inc and $dec on db directly but for the time_score, I retrieve data from db calculate the new score and write it back. What I'm worried about is that in case many users incremented the "score" field during my calculation of time_score then when I wrote time_score to db it'll corrupt the last value of score.

To be more clear does updating specific fields in a record in Mongo rewrites the whole record or only the updated fields ? (Assume that all these fields are indexed).


Here is the accepted answer: By default, whole documents are rewritten.  To specify the fields that are changed without modifying anything else, use the $set operator.

Edit: The comments on this answer are correct - any of the update modifiers will cause only relevant fields to be rewritten rather than the whole document.  By "default", I meant a case where no special modifiers are used (a vanilla document is provided).

Comment for this answer: Actually literally every update operator only modifies the field it is applied on. Overwriting the whole document is the exception rather than the rule as very few people want to overwrite the entire document in real world scenarious.
Comment for this answer: this is not accurate - when you use various operators (like $inc) only the specified field is changed.  the whole document is rewritten only when you pass in a full document for the update without any update operators.
Comment for this answer: Can anyone of you please post a link for me to verify that this is how the updates are done !!

Here is another answer: The algorithm you are describing is definitely not thread-safe.

When you read the entire document, change one field and then write back the entire document, you are creating a race condition - any field in the document that is modified after your read but before your write will be overwritten by your update.

That's one of many reasons to use $set or $inc operators to atomically set individual fields rather than updating the entire document based on possibly stale values in it.

Another reason is that setting/updating a single field "in-place" is much more efficient than writing the entire document.  In addition you have less load on your network when you are passing smaller update document ({$set:{field:value}}, rather than entire new version of the document).

Comment for this answer: the algorithm you describe is not thread safe for any operation that doesn't perform an atomic update.  $inc is safe but $set that's based on a possibly stale value is not safe.
Comment for this answer: No, you're wrong you didn't get my question I'm not saying I'll write the whole document I'm saying I don't wanna do this and the answer above is correct.
Comment for this answer: what if value is a calculation not a specific number ?, can I do something like this in mongo :

$set:{field: equation * field - another_field} ?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:11/20/12', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<mongodb>', 'Title': 'Does updating a MongoDB record rewrites the whole record or only the updated fields?', 'ViewCount': 1641, 'id': 13483251, 'user_AboutMe': '', 'user_AccountId': 1261832.0, 'user_DisplayName': 'Shehabic', 'user_Reputation': 6657.0, 'user_Views': 745.0, 'user_id': 2226557.0}}