0
0
MongoDBquery~20 mins

Schema versioning strategies in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Schema Versioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
1:30remaining
Output of schema version check query
Given a MongoDB collection users where each document has a schemaVersion field, what is the output of this query?

db.users.find({ schemaVersion: { $lt: 3 } }).count()

Assuming the collection has 5 documents with schemaVersion values: 1, 2, 3, 4, 5.
MongoDB
db.users.find({ schemaVersion: { $lt: 3 } }).count()
A3
B5
C2
D0
Attempts:
2 left
💡 Hint
Count documents where schemaVersion is less than 3.
🧠 Conceptual
intermediate
1:30remaining
Choosing a schema versioning strategy
Which schema versioning strategy is best when you want to keep old and new document formats in the same MongoDB collection and handle them differently in your application?
AAdd a <code>schemaVersion</code> field and write application logic to handle each version
BCreate a new collection for each schema version
COverwrite old documents with new schema without versioning
DUse MongoDB transactions to lock schema changes
Attempts:
2 left
💡 Hint
Think about how to identify document versions inside the same collection.
🔧 Debug
advanced
2:00remaining
Identify the error in schema migration script
This MongoDB migration script aims to update all documents from schemaVersion 1 to 2 by adding a new field status with value active. What error will this script cause?

db.users.updateMany(
  { schemaVersion: 1 },
  { $set: { status: 'active' }, $inc: { schemaVersion: 1 } }
)
AUpdate does not change <code>schemaVersion</code> field
BNo error, updates documents correctly
CRuntime error because <code>schemaVersion</code> is not numeric
DSyntaxError due to incorrect update operators usage
Attempts:
2 left
💡 Hint
Check if multiple update operators are combined correctly in one update document.
optimization
advanced
1:30remaining
Optimizing schema version queries
You have a large MongoDB collection with mixed schema versions. You often query documents with schemaVersion 3. Which index will optimize these queries best?
A{ schemaVersion: 1 }
B{ status: 1 }
C{ schemaVersion: 1, createdAt: -1 }
D{ createdAt: -1 }
Attempts:
2 left
💡 Hint
Index fields used in query filters for faster lookups.
🧠 Conceptual
expert
2:30remaining
Handling schema evolution with minimal downtime
What is the best approach to evolve MongoDB document schemas in a live system with zero downtime and backward compatibility?
ADrop the collection and recreate it with the new schema
BDisable writes during schema migration
CUpdate all documents in a single blocking migration operation
DUse a schemaVersion field and write application code to support multiple versions simultaneously
Attempts:
2 left
💡 Hint
Consider how to keep the system running while supporting old and new data formats.