0
0
MongoDBquery~20 mins

Choosing a good shard key in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shard Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is a shard key important in MongoDB?

Imagine you have a huge collection of documents in MongoDB. You want to split this data across many servers to keep things fast and balanced. Why do you need a shard key for this?

AIt stores user passwords securely.
BIt decides how data is split and helps queries find data quickly.
CIt compresses data to save disk space.
DIt backs up data automatically every hour.
Attempts:
2 left
💡 Hint

Think about how data is divided and found in many places.

query_result
intermediate
2:00remaining
What happens if you choose a shard key with low cardinality?

You have a collection where the shard key is a field with only two possible values. What is the likely result when you shard the collection on this key?

AQueries will run faster because of fewer shard key values.
BData will be perfectly balanced across all servers.
CMongoDB will reject the shard key due to low cardinality.
DData will be unevenly distributed, causing some servers to hold most data.
Attempts:
2 left
💡 Hint

Think about how many different values the shard key has and how data spreads.

📝 Syntax
advanced
2:30remaining
Which shard key definition is valid and efficient for a collection with user activity logs?

You want to shard a collection storing user activity logs. Each document has a userId and a timestamp. Which shard key is best to balance load and support queries filtering by user and time?

MongoDB
db.activity.createIndex({ userId: 1, timestamp: 1 })
A{ timestamp: 1 }
B{ userId: 1 }
C{ userId: 1, timestamp: 1 }
D{ activityType: 1 }
Attempts:
2 left
💡 Hint

Consider which key helps distribute data and supports common queries.

optimization
advanced
2:30remaining
How to avoid hotspotting with a shard key in MongoDB?

You notice one shard is overloaded because many writes target the same shard key value. What is a good way to avoid this hotspotting?

AChoose a shard key with high cardinality and randomness.
BUse a shard key with only a few distinct values.
CShard on a field that never changes.
DDisable sharding and use a single server.
Attempts:
2 left
💡 Hint

Think about spreading writes evenly across shards.

🔧 Debug
expert
3:00remaining
Why does this shard key choice cause unbalanced data?

A collection is sharded on the country field. Most documents have country set to 'USA'. What problem arises and why?

AMost data goes to one shard, causing imbalance and slow queries.
BMongoDB automatically balances data evenly regardless of shard key.
CThe shard key causes data duplication across shards.
DQueries fail because <code>country</code> is not indexed.
Attempts:
2 left
💡 Hint

Think about how many documents share the same shard key value.