nerdexam
MongoDB

C100DBA · Question #103

Write the command(s) are correct to enable sharding on a database "testdb" and shard a collection "testCollection" with _id as shard key. Answer: sh.enableSharding("testdb") & sh.shardCollection("test

The correct answer is A. sh.enableSharding("testdb") & sh.shardCollection("testdb.testCollection", {_id : 1 }, true ). Option A is correct because enabling sharding on a database and sharding a specific collection are two distinct, sequential steps in MongoDB - sh.enableSharding("testdb") tells the cluster to allow sharding on that database, and sh.shardCollection("testdb.testCollection", {_id: 1

Sharding

Question

Write the command(s) are correct to enable sharding on a database "testdb" and shard a collection "testCollection" with _id as shard key. Answer: sh.enableSharding("testdb") & sh.shardCollection("testdb.testCollection", {_id : 1 }, true )

Options

  • Ash.enableSharding("testdb") & sh.shardCollection("testdb.testCollection", {_id : 1 }, true )

How the community answered

(24 responses)
  • A
    100% (24)

Explanation

Option A is correct because enabling sharding on a database and sharding a specific collection are two distinct, sequential steps in MongoDB - sh.enableSharding("testdb") tells the cluster to allow sharding on that database, and sh.shardCollection("testdb.testCollection", {_id: 1}, true) shards the collection using _id as the shard key with the unique flag set to true. Since this is the only option presented, there are no distractors to evaluate, but common mistakes on similar questions include omitting the sh.enableSharding() step (required first), using the wrong namespace format (must be "db.collection" as a string), or forgetting that the shard key is passed as a document {_id: 1}, not just a field name.

Memory tip: Think of it as unlocking a building before assigning rooms - you must enable sharding on the database (unlock the building) before you can shard a collection (assign a specific room). The true third argument means "enforce uniqueness on the shard key," just like a unique index.

Topics

#enableSharding#shardCollection#shard key#sh commands

Community Discussion

No community discussion yet for this question.

Full C100DBA Practice