0
0
MongoDBquery~10 mins

Choosing a good shard key in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a sharded collection with a shard key on the 'userId' field.

MongoDB
sh.shardCollection("mydb.users", [1])
Drag options to blanks, or click blank then click option'
A{"userId": 1}
B{"userId": "hashed"}
C{"_id": 1}
D{"email": 1}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a document for the shard key.
Choosing a field that is not present in the documents.
2fill in blank
medium

Complete the code to shard the 'orders' collection using a hashed shard key on the 'orderId' field.

MongoDB
sh.shardCollection("shop.orders", [1])
Drag options to blanks, or click blank then click option'
A{"orderId": 1}
B{"orderId": "hashed"}
C{"customerId": "hashed"}
D{"_id": "hashed"}
Attempts:
3 left
💡 Hint
Common Mistakes
Using ascending (1) instead of 'hashed' for a hashed shard key.
Choosing a different field than intended.
3fill in blank
hard

Fix the error in the shard key specification to shard the 'products' collection on the 'category' field.

MongoDB
sh.shardCollection("store.products", [1])
Drag options to blanks, or click blank then click option'
A{"category": 1}
B{"category"}
C{"category": "asc"}
D"category: 1"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a document for the shard key.
Using an invalid index type like 'asc' or missing the value.
4fill in blank
hard

Fill both blanks to create a shard key on 'region' with ascending order and enable sharding on the database.

MongoDB
[1]
sh.shardCollection("mydb.customers", [2])
Drag options to blanks, or click blank then click option'
A{"region": 1}
B{"region": "hashed"}
Csh.enableSharding("mydb")
Dsh.shardCollection("mydb.customers", {"region": 1})
Attempts:
3 left
💡 Hint
Common Mistakes
Not enabling sharding on the database before sharding collections.
Using a hashed shard key when ascending is required.
5fill in blank
hard

Fill all three blanks to shard the 'logs' collection on 'timestamp' ascending, after enabling sharding on 'analytics' database.

MongoDB
sh.enableSharding([1])
sh.shardCollection([2], [3])
Drag options to blanks, or click blank then click option'
A"analytics"
B"analytics.logs"
C{"timestamp": 1}
D"logs"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the collection name without the database prefix.
Not enabling sharding on the database first.
Using incorrect shard key format.