Schema versioning strategies in MongoDB - Time & Space Complexity
When we change how data is stored in MongoDB, we use schema versioning to keep track of those changes.
We want to understand how the time to update or read data grows as the amount of data or versions grows.
Analyze the time complexity of migrating documents to a new schema version.
// Update all documents from old version to new version
const oldVersion = 1;
const newVersion = 2;
const result = db.collection.updateMany(
{ schemaVersion: oldVersion },
{ $set: { schemaVersion: newVersion, newField: "default" } }
);
This code updates all documents tagged with an old schema version to the new version by adding a new field.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The updateMany command scans and updates each matching document.
- How many times: Once for each document with the old schema version.
As the number of documents with the old version grows, the update operation takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 document updates |
| 100 | 100 document updates |
| 1000 | 1000 document updates |
Pattern observation: The time grows directly with the number of documents to update.
Time Complexity: O(n)
This means the time to update grows linearly with the number of documents that need schema changes.
[X] Wrong: "Updating schema versions is instant no matter how many documents there are."
[OK] Correct: Each document must be checked and updated, so more documents mean more work and more time.
Understanding how schema updates scale helps you design better data migrations and shows you think about real-world data growth.
"What if we added an index on the schemaVersion field? How would that affect the time complexity of the update?"