Reputation: 1385
We have a MongoDB with hundreds of thousands of documents, where each of them has multiple fields including one array that will increase over time (one additional entry per day, where each entry contains an integer and a date).
To optimize the speed of updating the documents daily, would it make sense to reserve more disk space per document so that the document doesn't need to be moved each time we add an entry to the array?
Upvotes: 0
Views: 555
Reputation: 20703
Preallocating space does not help you too much in this situation. Since your documents sooner or later will increase beyond the padding space, there will be document migrations, anyway. But with a high padding factor, you increase the overhead in terms of disk usage, and potentially a great deal so.
Your data model is flawed. For a field potentially growing to infinity, you should use a second collection for the daily events, referring to the base document.
{
_id: baseDocId,
…
}
would be your base document, then, and
{
_id: dailyEventId,
base: baseDocId,
date: someISODate,
…
}
a document for a single event. It is still easy to get all events for a known base
db.events.find({base: baseDocId})
but you have no problems with document migrations when adding a new event, do not need to increase your padding overhead and you get past the 16MB document size limit, too.
Upvotes: 1