Complete the code to specify the shard key for horizontal scaling in MongoDB.
sh.shardCollection("mydb.mycollection", { [1]: 1 })
The shard key determines how data is distributed across shards. Using userId is common for user-based data partitioning.
Complete the code to add a new shard to the MongoDB cluster.
sh.addShard("[1]")
The addShard command requires the shard's replica set name and host address, like shard0001/mongo1:27017.
Fix the error in the command to enable sharding on a database.
sh.enableSharding("[1]")
Sharding is enabled on user databases like mydb. System databases like admin, config, and local cannot be sharded.
Fill both blanks to create a shard key with a compound index for better data distribution.
sh.shardCollection("mydb.orders", { [1]: 1, [2]: 1 })
A compound shard key with customerId and orderDate helps distribute data by user and time, improving balance and query efficiency.
Fill all three blanks to configure a MongoDB sharded cluster with a config server, shard, and mongos router.
config = new Mongo("[1]") sh.addShard("[2]") mongos = new Mongo("[3]")
The config server runs on configsvr1:27019, the shard is shard0002/mongo2:27017, and the mongos router is mongos1:27017.