Mongo 5+ writes during update of one field in a document

Greetings,

I would like to know if Mongo 5+ performs as much write if we update one field in a document or many fields. If we have the following document:

db.products.insertOne(
{
_id: 100,
quantity: 250,
instock: true,
reorder: false,
details: { model: “14QQ”, make: “Clothes Corp” },
tags: [ “apparel”, “clothing” ],
ratings: [ { by: “Customer007”, rating: 4 } ]
}
)

Then we execute the following update:

db.products.updateOne(
{ _id: 100 },
{ $set:
{
quantity: 500,
details: { model: “2600”}
}
}
)

Will Mongo have to write only the updated value of “500” and “2600” or does it update those two values, but also has to rewrite the other fields (_id, instock,reorder,details:{model:“14QQ”}, tags, and ratings) even though they weren’t updated by updateOne?

This information will affect how we model the data. I hope only the update values have to be written to disk.

Thanks for your help,

John

You should not base your schema on implementation details of the database server you are using.

Only the fields that differs are updated.

But despite the fact that only the fields that differ are updated, the whole document is written back to disk. And that’s for efficiency reasons, compression would be hard on a field by field basis.

Could you please provide closure on some of your other threads by marking some post as the solution?

That helps the forum being useful by indicating to readers that they can trust the answer provided.

Thanks, Stevie.

Wow, the whole document is rewritten despite updating only one field in the document ! This is something that I need to think over. I’m new to Mongo, but if we had a document that that has multiple nested documents and say one reaches over 100 fields with each document sized at over 10 MB on average, and our app only updated one number field millions of times per day, then Mongo would have to write 10MB over and over. I’m thinking at that time I will might consider using linked collections instead of embedding them in the same collection.

P.S. thanks for letting me know that I need to close postings. I never had to do that with other forums. I’ll proceed to close those I know have a solution.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.