0
0
MongoDBquery~10 mins

Shard key selection importance 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 specify the shard key when sharding a collection.

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

Complete the code to create an index on the shard key field.

MongoDB
db.users.createIndex({ [1]: 1 })
Drag options to blanks, or click blank then click option'
Aage
Blocation
Cemail
DuserId
Attempts:
3 left
💡 Hint
Common Mistakes
Creating an index on a non-shard key field.
Forgetting to create an index on the shard key.
3fill in blank
hard

Fix the error in the shard key selection to avoid unbalanced data distribution.

MongoDB
sh.shardCollection("mydb.orders", { [1]: 1 })
Drag options to blanks, or click blank then click option'
AcustomerId
BorderDate
Cstatus
DtotalAmount
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field with few distinct values as shard key.
Choosing a field not used in queries.
4fill in blank
hard

Fill both blanks to create a compound shard key for better data distribution.

MongoDB
sh.shardCollection("mydb.logs", { [1]: 1, [2]: 1 })
Drag options to blanks, or click blank then click option'
AuserId
Btimestamp
Cregion
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing fields that do not improve data distribution.
Using only one field when a compound key is better.
5fill in blank
hard

Fill all three blanks to define a shard key and create an index for efficient queries.

MongoDB
sh.shardCollection("mydb.sales", { [1]: 1, [2]: 1 });
db.sales.createIndex({ [3]: 1 })
Drag options to blanks, or click blank then click option'
Aregion
BproductId
DsaleDate
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same field for all blanks.
Not creating an index on a frequently queried field.