However, you can also choose to connect to a live MongoDB database. Do so and you’ll never have to worry about configuring the database connection during deployment. That’s where the MongoDB cluster comes in.
But how can you set up a MongoDB cluster and get a connection string for your app? You’ll find out in this guide.
What Is MongoDB Cluster and Why Should You Use It?
MongoDB Atlas cluster is a cloud storage option for hosting a MongoDB database. You can host MongoDB on any server. But when you use the MongoDB cluster cloud service, it lets you store your data on either AWS, Google Cloud, or Azure. MongoDB cluster is also language-agnostic. So you can use it with any language.
One of the advantages of the MongoDB Atlas cluster is you don’t need to worry about cloud service configurations, as it handles this for you. And while there are paid options for the service, you can start with the free shared tier. You can then scale up to a premium plan later, once your app’s needs increase.
How to Set Up a MongoDB Atlas Cluster
Setting up a MongoDB cluster requires only a few configurations here and there. In the end, all you need is a connection string to create a MongoDB database and collections. To start, you only need to register on the MongoDB website and create a cluster. Then get a connection string.
Create a Cluster
A cluster is a container for your databases, and you can create several databases inside it.
To create a cluster:
Head over to the MongoDB Atlas platform. Tap Sign In at the top-right. Then follow the onscreen instructions to create an account. You might want to use the Google authentication option to make this easier. Accept the privacy policy when asked. Complete the form on the Welcome to Atlas page. Then click Finish. MongoDB Atlas now takes you to a page to select a cloud plan. From the options, select the Free Shared tier and click Create. Note: If you’ve skipped the above step earlier and find yourself in the dashboard, click Build a Database to revert to the plans page. Otherwise, move to the next step. Next, choose any of the cloud service options on the next page. Ensure that there’s a highlight on the Free Shared option box. We’ll go with AWS in this case. Click Create Cluster. Select Username and Password as your authentication method, then fill the Username and Password fields. You can click Autogenerate Secure Password to generate a random password. If you do, remember to copy it and keep it in a secure place, like a password manager. Click Create User. Scroll down and select My Local Environment. Click Add My Current IP Address under the Add entries to your IP Access List section. Click Finish and Close. Click Go to Databases from the modal pop-up.
That’s it for cluster creation.
Get a Connection String From Your Cluster
A connection string is a URI that links your app with the MongoDB Atlas cluster.
To get one:
On the database dashboard, click Connect. Select Connect your application. Click the dropdown under the DRIVER section and select your development language or technology. Also, choose your technology version under VERSION. Tick the Include full driver code example box to get an example connection code block.
You’ll now see a block of code in a preformatted area. You can simply copy and hook this into your code. However, you might want to extract the URI if you’re using a custom connection method other than the one in the provided code. Unticking the Include full driver code example leaves you with only the connection string.
It typically looks like this:
MongoDB cluster will append your username to the string automatically. You’ll need to replace
For example, if your username, password, and database name are admin, secret_password, and myDB, the connection string becomes:
Below is some sample code demonstrating a MongoDB connection in Node.js. It uses the mongoose npm package as its model manager.
To use this function inside your Node.js project’s server:
Consider reading the connection string from a .env file as best practice. This helps hide your access username and password from threat actors during production.
Go back to your dashboard and click Browse Collections to see your databases and collections. You’ll only see these when you store data in your cluster.
MongoDB Atlas Cluster Makes Data Storage Easier
MongoDB is a popular NoSQL database used for storing binary fields and JSON documents. Its cluster cloud service is one of its strong points. It only requires you to connect your app with it, and you can query the database as you would if it was in a local environment. Additionally, using the MongoDB cluster helps declutter your production server.