0
0
MongoDBquery~20 mins

Shard key selection importance in MongoDB - Practice Problems & Coding Challenges

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

In MongoDB, choosing the right shard key affects how data is distributed and accessed. Which of the following best explains why selecting an appropriate shard key is crucial?

AIt controls the encryption level of the data stored in the cluster.
BIt sets the backup frequency for each shard automatically.
CIt determines how data is partitioned across shards, impacting query speed and cluster balance.
DIt decides the maximum size of each document in the database.
Attempts:
2 left
💡 Hint

Think about how data is split and accessed in a distributed system.

query_result
intermediate
2:00remaining
What is the output of this shard key usage scenario?

Consider a MongoDB collection sharded on the field userId. If queries mostly filter by userId, what is the expected effect on query performance?

AQueries will be efficient because they target specific shards based on <code>userId</code>.
BQueries will be slow because all shards must be scanned for <code>userId</code>.
CQueries will fail because <code>userId</code> is not a valid shard key.
DQueries will be efficient only if <code>userId</code> is indexed but not sharded.
Attempts:
2 left
💡 Hint

Think about how shard keys help route queries.

📝 Syntax
advanced
2:00remaining
Identify the correct shard key creation command

Which MongoDB command correctly creates a sharded collection with orderId as the shard key?

MongoDB
sh.enableSharding("salesDB");
sh.shardCollection("salesDB.orders", {orderId: 1});
Ash.shardCollection("salesDB.orders", {orderId: "asc"})
Bsh.shardCollection("salesDB.orders", "orderId")
Csh.shardCollection("salesDB.orders", {orderId})
Dsh.shardCollection("salesDB.orders", {orderId: 1})
Attempts:
2 left
💡 Hint

Look for the correct syntax for specifying shard keys as a document with field and direction.

optimization
advanced
2:00remaining
Which shard key choice leads to better cluster balance?

You have a collection with a status field that has only three possible values: 'new', 'processing', and 'done'. You want to shard this collection. Which shard key choice will likely lead to better data distribution?

AShard on a compound key: <code>{status: 1, createdAt: 1}</code>.
BShard on <code>status</code> field alone.
CShard on <code>createdAt</code> field alone.
DShard on a constant value field.
Attempts:
2 left
💡 Hint

Think about how many unique values the shard key has and how that affects distribution.

🔧 Debug
expert
3:00remaining
Why does this shard key choice cause a performance problem?

A collection is sharded on the timestamp field, which is always increasing. Over time, queries slow down and one shard becomes overloaded. What is the main reason for this problem?

AThe shard key has too many unique values, causing overhead.
BThe shard key is monotonically increasing, causing all inserts to go to one shard, creating a hotspot.
CThe shard key is not indexed, causing slow queries.
DThe shard key is a compound key, which MongoDB does not support.
Attempts:
2 left
💡 Hint

Consider how data is distributed when the shard key values increase over time.