WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

MongoDB Β· Intermediate Β· question 35 of 100

Describe the process of horizontal scaling in MongoDB using sharding.?

πŸ“• Buy this interview preparation book: 100 MongoDB questions & answers β€” PDF + EPUB for $5

Horizontal scaling in MongoDB is achieved using sharding. Sharding is the process of distributing data across multiple servers, called shards, in order to improve performance and accommodate larger data sets. In this process, data is divided into small, manageable chunks and distributed across the shards.

To implement sharding in MongoDB, we need to follow the following steps:

1. Install and configure MongoDB: First, we need to install MongoDB on each shard server and configure them to communicate with each other. We also need to enable sharding in the MongoDB configuration file.

2. Choose a sharding key: We need to choose a sharding key that represents the data that we want to distribute across the shards. The sharding key should be chosen based on the access patterns of the data and the size of the data set.

3. Enable sharding for the database: We need to enable sharding for the specific database that we want to shard. We can do this by connecting to the mongos instance and using the sh.enableSharding() command.

4. Create a sharded collection: Next, we need to create a sharded collection in the database. We can do this using the sh.shardCollection() command, which specifies the collection, sharding key and the number of shards to use.

5. Add shards: We need to add shards to the cluster using the sh.addShard() command, which specifies the hostname and port number of the shard server.

Once the above steps are completed, data will be automatically distributed across the shards based on the sharding key we have chosen. When a query is executed, the mongos instance will determine which shard contains the required data and route the query to that shard. The results are then aggregated and returned to the client.

For example, suppose we have a database of customer records with fields such as name, age, address and phone number. We can choose the age field as the sharding key since it is likely to be used in many queries and evenly distributed across the data set. We enable sharding for the database and create a sharded collection called customers using the age field as the sharding key. We add a few shards to the cluster and insert customer records into the customers collection. When a query is executed to find all customers with age greater than 30, the mongos instance routes the query to the shard containing the required data, aggregates the results and returns them to the client.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview β€” then scores it.
πŸ“ž Practice MongoDB β€” free 15 min
πŸ“• Buy this interview preparation book: 100 MongoDB questions & answers β€” PDF + EPUB for $5

All 100 MongoDB questions Β· All topics