Complete the code to specify the shard key when sharding a collection.
sh.shardCollection("mydb.users", [1])
The shard key must be specified as a document indicating the field(s) to shard on. Here, { userId: 1 } is the correct shard key.
Complete the code to create an index on the shard key field.
db.users.createIndex({ [1]: 1 })The shard key field must be indexed for efficient query routing. Here, userId is the shard key field.
Fix the error in the shard key selection to avoid unbalanced data distribution.
sh.shardCollection("mydb.orders", { [1]: 1 })
Choosing customerId as the shard key helps distribute data evenly because it has high cardinality and is frequently used in queries.
Fill both blanks to create a compound shard key for better data distribution.
sh.shardCollection("mydb.logs", { [1]: 1, [2]: 1 })
A compound shard key with timestamp and region helps distribute data by time and location, improving balance and query performance.
Fill all three blanks to define a shard key and create an index for efficient queries.
sh.shardCollection("mydb.sales", { [1]: 1, [2]: 1 }); db.sales.createIndex({ [3]: 1 })
The shard key is a compound key on region and productId. The index on saleDate helps queries filter by date efficiently.