Reputation: 6114
We're building a new system using MongoDB for the first time. One of our main document collections contains documents that can be anywhere in size from a few hundred bytes, all the way up to near the 16MB document limit.
We need to track the number of times a document has been read and limit the read count.
We're weighing two options:
The first has value in that we could get the count and the data in one hit to the database.
The part that makes this concerning is that it seems that document size could affect write performance. However, I have not found any reference to that being the case. Our speculation is that even if we're updating a single field, the entire document would have to be deserialized, updated, serialized, and written back to the table. If the document size varies greatly, then it seems that performance would follow suit. Additionally, the collection will be indexed, and MongoDB documentation says that every update requires all indexes on the collection to be updated. These all add up to potential performance problems.
If so, then we would go with the second option and make two database hits.
Upvotes: 3
Views: 1391
Reputation: 222751
Addressing both of your concerns, update of the document doesn't depend on the size of the document you are updating*. So if you have two documents: one with 200 fields which weight 6Mb and another with 3 fields which weight 0.4Kb and you need to update 2 fields for both of the documents, both of them will take approximately the same time.
I added a star there, because if you are updating the document and the document became bigger then original there is a possibility that the document will be moved (if there is not enough space to accomodate the new document). In such case the size of the document will add some penalty (which you can mitigate with padding). As told by Thilio atomic updates never result in document movements.
Regarding your index concern. Index does not depend on the document size. It depends on the number of fields you are indexing and the size of the fields. For example an index for a field which has "a very very long text will be longer" then the field with "short text". And the size of index of an array [1, 2, ..., 500] will be longer then [1, 2].
Upvotes: 1
Reputation: 262714
If you use the "atomic update" operators and the field you are updating does not change in size, then the update can be performed without touching the rest of the document. That would work with $inc
for example.
Regarding indexes, if you don't have an index on the field that you are updating here, then they won't have to be updated.
Upvotes: 3