MCQs Of Day 21: MongoDB Sharding (Day 21)
1. What is the purpose of sharding in MongoDB?
A) To distribute data across multiple nodes for horizontal scaling
B) To improve query performance on a single node
C) To store data in encrypted format
D) To increase the amount of disk space available on a single server
Answer: A) To distribute data across multiple nodes for horizontal scaling
Explanation: Sharding in MongoDB helps in distributing large datasets across multiple servers or nodes, which enables horizontal scaling.
2. What does a shard in MongoDB store?
A) Configuration data
B) Query routing information
C) Actual application data
D) Data replication logs
Answer: C) Actual application data
Explanation: Shards in a MongoDB cluster store the actual data of the application and are responsible for data partitioning.
3. What are config servers in a MongoDB sharded cluster responsible for?
A) Storing application data
B) Routing queries to appropriate shards
C) Storing metadata and configuration settings
D) Managing the replica sets
Answer: C) Storing metadata and configuration settings
Explanation: Config servers store metadata and configuration settings for a sharded cluster to manage how data is distributed across shards.
4. Which component in a sharded MongoDB cluster routes queries to appropriate shards?
A) Shard
B) Config Server
C) Query Router (mongos)
D) Replica Set
Answer: C) Query Router (mongos)
Explanation: The query router, also known as mongos, is responsible for routing client queries to the correct shard based on metadata stored in the config servers.
5. In MongoDB sharding, what is the shard key used for?
A) To index the database
B) To authenticate users
C) To determine how data is distributed across shards
D) To manage replication
Answer: C) To determine how data is distributed across shards
Explanation: The shard key is used to partition the data across different shards, and it determines how documents are distributed within the sharded cluster.
6. What type of key is commonly used as a shard key in MongoDB?
A) Primary key
B) Foreign key
C) Shard key
D) Unique or indexed field
Answer: D) Unique or indexed field
Explanation: A shard key is usually a unique or indexed field that is frequently queried to ensure efficient data distribution and retrieval.
7. Which of the following is NOT a common method of sharding in MongoDB?
A) Range-based sharding
B) Hash-based sharding
C) Field-based sharding
D) Zone-based sharding
Answer: C) Field-based sharding
Explanation: MongoDB commonly uses range-based or hash-based sharding. "Field-based sharding" is not a typical method.
8. Which of the following is a disadvantage of range-based sharding?
A) Uneven data distribution
B) Random distribution of data
C) Complexity of key selection
D) Slow query routing
Answer: A) Uneven data distribution
Explanation: Range-based sharding can lead to uneven data distribution, especially if queries are concentrated on specific value ranges.
9. In hash-based sharding, how is the data distributed?
A) By a range of values
B) Based on a hash of the shard key
C) By a random selection of fields
D) Based on the document size
Answer: B) Based on a hash of the shard key
Explanation: Hash-based sharding distributes data by applying a hash function to the shard key, ensuring a more uniform distribution across shards.
10. Which of the following is used to monitor the performance of a MongoDB sharded cluster?
A) MongoDB shell
B) mongostat
C) MongoDB compass
D) MongoDB backup utility
Answer: B) mongostat
Explanation: mongostat is a command-line tool used to monitor the performance of MongoDB instances, including sharded clusters, by displaying real-time statistics.
11. Which component of a MongoDB sharded cluster is responsible for managing data replication?
A) Shards
B) Config servers
C) Query routers (mongos)
D) Replica sets
Answer: D) Replica sets
Explanation: Replica sets are responsible for managing data replication in MongoDB. Each shard in a sharded cluster is typically a replica set to ensure data availability.
12. What happens when a shard fails in a sharded MongoDB cluster?
A) The data becomes permanently unavailable
B) MongoDB automatically reroutes requests to other shards
C) Data is transferred to another cluster
D) The system crashes
Answer: B) MongoDB automatically reroutes requests to other shards
Explanation: MongoDB automatically reroutes requests to other available shards, especially if the failed shard is part of a replica set.
13. How many config servers are typically used in a MongoDB sharded cluster?
A) One
B) Two
C) Three
D) Four
Answer: C) Three
Explanation: MongoDB typically uses three config servers for a sharded cluster to ensure redundancy and fault tolerance.
14. What is the mongos process in a MongoDB sharded cluster responsible for?
A) Distributing data across shards
B) Storing metadata
C) Routing client requests to the appropriate shard
D) Handling replication
Answer: C) Routing client requests to the appropriate shard
Explanation: The mongos process is responsible for routing queries from clients to the appropriate shard based on the metadata stored in the config servers.
15. Which of the following is an important factor when choosing a shard key in MongoDB?
A) High cardinality
B) Low cardinality
C) Random distribution of values
D) Limited query use
Answer: A) High cardinality
Explanation: A shard key with high cardinality (many unique values) helps distribute data evenly across shards and reduces the chance of hotspots.
16. What type of MongoDB collection can be sharded?
A) Only collections with indexes
B) Only collections with more than 1GB of data
C) All collections
D) Collections that use replica sets
Answer: C) All collections
Explanation: Any collection in a MongoDB database can be sharded, as long as a shard key is defined for it.
17. What does the sh.enableSharding() command do in MongoDB?
A) Enables sharding for a specific database
B) Enables replication for a shard
C) Adds a new shard to the cluster
D) Initializes a query router
Answer: A) Enables sharding for a specific database
Explanation: The sh.enableSharding() command is used to enable sharding for a specific database, which allows its collections to be sharded.
18. What does the sh.shardCollection() command do in MongoDB?
A) Enables sharding for a collection
B) Specifies the shard key for a collection
C) Adds a collection to the cluster
D) Moves data from one shard to another
Answer: B) Specifies the shard key for a collection
Explanation: The sh.shardCollection() command defines the shard key for a collection, which determines how the collection is distributed across shards.
19. What type of sharding method is best for queries that involve ranges of values?
A) Hash-based sharding
B) Range-based sharding
C) Random-based sharding
D) None of the above
Answer: B) Range-based sharding
Explanation: Range-based sharding is ideal for queries involving range-based operations, such as querying for records within a specific date range.
20. What is a "hotspot" in a sharded MongoDB cluster?
A) A shard that has too much data
B) A shard that is frequently queried and becomes overloaded
C) A shard that has no data
D) A configuration error
Answer: B) A shard that is frequently queried and becomes overloaded
Explanation: A hotspot occurs when a specific shard is frequently accessed and becomes a bottleneck, affecting performance.
21. How is a range-based sharding strategy typically implemented in MongoDB?
A) By hashing the shard key
B) By selecting a range of values for the shard key
C) By distributing documents evenly across all shards
D) By setting the shard key to a random value
Answer: B) By selecting a range of values for the shard key
Explanation: Range-based sharding divides data into chunks based on specified ranges of the shard key values.
22. What is the main benefit of using hash-based sharding?
A) Better support for range queries
B) More uniform data distribution across shards
C) Easier to implement than range-based sharding
D) Better indexing capabilities
Answer: B) More uniform data distribution across shards
Explanation: Hash-based sharding ensures that data is evenly distributed across all shards by applying a hash function to the shard key.
23. Which command is used to add a shard to a MongoDB sharded cluster?
A) sh.addShard()
B) sh.addShardCluster()
C) mongod --shard
D) sh.enableSharding()
Answer: A) sh.addShard()
Explanation: The sh.addShard() command is used to add new shards to an existing MongoDB sharded cluster.
24. What is one potential drawback of hash-based sharding?
A) Uneven data distribution
B) Increased complexity in data retrieval
C) Limited ability to handle large datasets
D) Increased risk of hotspots
Answer: B) Increased complexity in data retrieval
Explanation: Hash-based sharding can increase the complexity of range queries since data is distributed randomly across shards, making it harder to perform efficient range-based queries.
25. What is the maximum number of shards a MongoDB cluster can support by default?
A) 10
B) 50
C) 100
D) There is no limit
Answer: D) There is no limit
Explanation: MongoDB can scale to support a large number of shards, and there is no fixed limit for the number of shards in a cluster.
26. Which of the following is required to set up sharding in MongoDB?
A) A replica set
B) A config server
C) A dedicated sharded server
D) A query router
Answer: B) A config server
Explanation: Config servers are required to store metadata and configuration settings for the sharded MongoDB cluster.
27. What does the sh.status() command do in a MongoDB sharded cluster?
A) Displays information about the current status of sharding in the cluster
B) Shuts down the sharded cluster
C) Displays the current shard key settings
D) Displays detailed logs of the sharded cluster
Answer: A) Displays information about the current status of sharding in the cluster
Explanation: The sh.status() command shows the current status of the sharded cluster, including the configuration of the shards and the distribution of data.
28. Which of the following types of queries is most efficient in a sharded MongoDB cluster?
A) Queries that filter by the shard key
B) Queries that do not include the shard key
C) Range queries that span multiple shards
D) Aggregation queries that require joining multiple collections
Answer: A) Queries that filter by the shard key
Explanation: Queries that filter by the shard key are more efficient since they can be directed to the specific shard holding the relevant data, reducing the number of shards involved.
29. In MongoDB sharding, what does a chunk represent?
A) A set of documents within a single shard
B) A group of related shards
C) A subset of data from a collection
D) A segment of the shard key
Answer: C) A subset of data from a collection
Explanation: A chunk represents a subset of data that is divided based on the shard key value and distributed across shards.
30. What should be avoided when selecting a shard key for MongoDB sharding?
A) Using a field with high cardinality
B) Using a field frequently queried
C) Using a field with low cardinality or too few unique values
D) Using an indexed field
Answer: C) Using a field with low cardinality or too few unique values
Explanation: A shard key with low cardinality can lead to uneven data distribution, causing hotspots and performance issues.
31. What is the recommended number of config servers for a MongoDB sharded cluster?
A) 1
B) 2
C) 3
D) 5
Answer: C) 3
Explanation: The recommended configuration for MongoDB sharded clusters is to use three config servers for redundancy and fault tolerance.
32. How can you check whether a MongoDB collection is sharded?
A) Use db.collection.isSharded()
B) Check the shard key settings in the config servers
C) Use the sh.isSharded() command
D) There is no way to check
Answer: A) Use db.collection.isSharded()
Explanation: The isSharded() method can be used to check if a collection is sharded in a MongoDB cluster.
33. What command is used to enable sharding on a specific database?
A) sh.enableDatabase()
B) sh.enableSharding()
C) sh.startSharding()
D) sh.addDatabase()
Answer: B) sh.enableSharding()
Explanation: The sh.enableSharding() command is used to enable sharding on a specific database, allowing it to be part of a sharded cluster.
34. Which component is responsible for keeping track of all the chunks in a MongoDB sharded cluster?
A) Shard
B) Query Router
C) Config Servers
D) MongoDB Shell
Answer: C) Config Servers
Explanation: Config servers keep track of metadata and the distribution of chunks across shards in the MongoDB cluster.
35. What is a potential issue of range-based sharding?
A) Hotspots and uneven data distribution
B) Limited query efficiency
C) Data redundancy
D) Complicated data retrieval
Answer: A) Hotspots and uneven data distribution
Explanation: Range-based sharding can lead to hotspots if large portions of the data fall within the same range, resulting in uneven distribution across shards.
36. What would you do if a MongoDB shard key has too many duplicate values?
A) Change the shard key to a more unique field
B) Delete all documents with duplicate values
C) Increase the size of the shard key
D) Use hash-based sharding
Answer: A) Change the shard key to a more unique field
Explanation: If a shard key has too many duplicate values, it may lead to hotspots. A more unique field should be chosen to ensure even data distribution.
37. Which of the following is a valid shard key for MongoDB?
A) A field that is not indexed
B) A field with a high number of unique values
C) A field with low cardinality
D) A field that stores large documents
Answer: B) A field with a high number of unique values
Explanation: A field with high cardinality helps ensure uniform data distribution, avoiding hotspots and improving performance.
38. Which of the following is NOT a valid sharding strategy in MongoDB?
A) Range-based sharding
B) Hash-based sharding
C) Geo-based sharding
D) Zone-based sharding
Answer: C) Geo-based sharding
Explanation: While MongoDB offers geospatial queries, "Geo-based sharding" is not a common strategy for partitioning data.
39. What command is used to view the current distribution of chunks in a MongoDB sharded cluster?
A) sh.chunkStatus()
B) sh.status()
C) db.getChunks()
D) db.getShards()
Answer: B) sh.status()
Explanation: The sh.status() command displays the current status of the sharded cluster, including chunk distribution and shard details.
40. How does MongoDB handle write operations in a sharded cluster?
A) Write operations are sent to all shards
B) Write operations are sent only to the primary shard of the cluster
C) Write operations are sent to a specific shard based on the shard key
D) Write operations are sent to the config servers
Answer: C) Write operations are sent to a specific shard based on the shard key
Explanation: Write operations are directed to the shard that contains the relevant data based on the shard key.
41. Which of the following MongoDB features is automatically enabled when using sharding?
A) Replica sets
B) Data encryption
C) Automatic balancing
D) Full-text search
Answer: C) Automatic balancing
Explanation: Sharded MongoDB clusters have automatic balancing enabled by default, ensuring that data is evenly distributed across all available shards.
42. What is the role of a mongos process in a MongoDB sharded cluster?
A) It acts as a config server
B) It manages the shard key distribution
C) It routes client requests to the appropriate shard
D) It stores the data on disk
Answer: C) It routes client requests to the appropriate shard
Explanation: The mongos process is a query router that directs client requests to the appropriate shard in a sharded MongoDB cluster.
43. What happens when a MongoDB shard becomes unavailable in a sharded cluster?
A) The entire cluster becomes unavailable
B) The data stored in the shard is lost
C) MongoDB automatically reroutes requests to other shards
D) Only read operations are affected
Answer: C) MongoDB automatically reroutes requests to other shards
Explanation: If a shard becomes unavailable, MongoDB reroutes requests to other available shards, ensuring continued operation, provided replica sets are used.
44. Which of the following operations is NOT supported in a MongoDB sharded cluster?
A) Joins across multiple shards
B) Aggregation with $lookup across shards
C) Full-text search
D) Transactions across multiple shards
Answer: A) Joins across multiple shards
Explanation: While MongoDB supports some join-like operations, such as $lookup, performing them across multiple shards can be inefficient and is not fully supported for certain types of operations.
45. What is the primary purpose of the mongos process in MongoDB?
A) It handles authentication for the cluster
B) It stores shard data
C) It routes client requests to the appropriate shard
D) It manages the database schema
Answer: C) It routes client requests to the appropriate shard
Explanation: The mongos process acts as a query router, directing requests to the correct shard or shards in the MongoDB cluster based on the shard key.
46. In a sharded MongoDB cluster, how are write operations distributed across shards?
A) Write operations are always sent to the primary shard
B) Write operations are distributed randomly across shards
C) Write operations are routed to the appropriate shard based on the shard key
D) Write operations are replicated to all shards
Answer: C) Write operations are routed to the appropriate shard based on the shard key
Explanation: Write operations are routed to the shard that contains the relevant data, determined by the shard key.
47. What is a key benefit of using sharding in MongoDB?
A) Reduced storage requirements
B) Improved consistency across nodes
C) Horizontal scalability and the ability to handle large datasets
D) Faster query execution times for all queries
Answer: C) Horizontal scalability and the ability to handle large datasets
Explanation: Sharding allows MongoDB to scale horizontally, distributing data across multiple servers, which enables it to handle large datasets efficiently.
48. What happens if a shard key is chosen poorly in MongoDB sharding?
A) Data will be distributed evenly across all shards
B) Data will be distributed based on the primary key
C) There may be hotspots and uneven data distribution
D) The database will automatically choose a better shard key
Answer: C) There may be hotspots and uneven data distribution
Explanation: A poorly chosen shard key, such as one with low cardinality or an uneven distribution of values, can lead to hotspots and uneven data distribution across the shards.
49. What is a "zone" in MongoDB sharding?
A) A group of replica sets
B) A specific range of shard keys
C) A collection of related chunks
D) A set of distributed databases
Answer: B) A specific range of shard keys
Explanation: A zone in MongoDB sharding represents a specific range of shard key values that are assigned to particular shards, allowing for more control over data distribution.
50. What is the sh.removeShard() command used for in MongoDB?
A) To remove a shard from the cluster
B) To remove a collection from a shard
C) To delete chunks from a shard
D) To disable sharding on a specific database
Answer: A) To remove a shard from the cluster
Explanation: The sh.removeShard() command is used to remove a shard from a MongoDB cluster. It may involve redistributing chunks of data from the shard to others in the cluster.
