Reputation: 4065
I have a counter that I want to persist between requests on my web server and figured I could just stick it into a MongoDB database, because it already exists.
I am expecting an update to this counter about every 1-3 seconds.
However, I read that whenever you save a document in MongoDB the document gets deleted and re-inserted. And I am wondering if the same applies when you do a $set, without updating the entire document?
Also, if that is the case, would I expect any particular performance impact on the database?
Upvotes: 0
Views: 76
Reputation: 312045
An update that uses $set
to modify one or more fields of an existing doc will only update the affected fields. The update occurs in-place unless the changes cause the size of the document to increase beyond what's currently allocated for it. When that happens the doc needs to be relocated; effectively deleting and re-creating the doc.
From the docs:
For the MMAPv1 storage engine, if an update operation causes a document to exceed the currently allocated record size, MongoDB relocates the document on disk with enough contiguous space to hold the document. Updates that require relocations take longer than updates that do not, particularly if the collection has indexes. If a collection has indexes, MongoDB must update all index entries. Thus, for a collection with many indexes, the move will impact the write throughput.
Upvotes: 1