MongoDB is the most popular NoSQL database management program and is a part of many popular stacks like MERN, MEAN, and MEVN. MongoDB is completely open -source and free to use, but for deployment, we generally need to take the paid route. We can also download the community edition of MongoDB locally and use it through the command line or the nice graphical interface of MongoDB Compass.
For deployment, we need a Linux based server. We can either use our own server or deploy it in any of the available, professionally managed cloud services. Three popular options are to deploy in a linode server, Heroku, or AWS.
While all of these are external services, MongoDB also gives its own cloud-based service of MongoDB Atlas which is easier than all the above-mentioned services. We will take you through that in this article.
Introducing MongoDB Atlas
MongoDB Atlas is a Database as a Service (DBaaS), provided by the team behind MongoDB. It is a fully automated service with minimal to no configuration. Additionally, you have an option to deploy MongoDB instances in the any of the top three cloud providers, which are AWS, Azure, or Google cloud. It is an easy-to-use cloud-based service, which was released in 2016 and has been battle tested since. It is used and loved by both start-ups and many well-established enterprises like Invision, Ebay, Adobe, and Google.
Feature Rich Deployment
Although MongoDB Atlas is fully automated, it provides a very feature rich deployment. The moment we create a MongoDB instance, the Built-in replication kicks and our data is now stored at multiple locations. It is always available, even when the master is down.
- It provides us Automated Security features, through which we can keep an eye on who is using our data and keep the bad actors out.
- It provides a good option for automatic backup and recovery. Even if our data is corrupted, we have a dependable recovery option.
- Through the dashboard, we get a lot of information using which, we can monitor everything and decide when to upgrade our plan.
- Getting Started with Atlas
Getting Started with Atlas
We will now learn to create a MongoDB Atlas account and create a database in it. First, go to https://www.mongodb.com/atlas and click on the ‘Try Free’ button.
In the next page, it will ask you to Sign up or Sign in. You can also use your google account to do so.
Since I already have an account, I clicked on the ‘Sign in’ option and the following page came up.
If you already have a project on MongoDB Atlas, you will be taken to the project on which you last worked. Here, you need to click on the project, and then in the pop-up, click on New Project.
Then, it will ask us to give the project a name. I have given the name ‘employees’.
In the next screen, it will ask you to give access to members. I have given access to the existing users. After that, you need to click on Create Project button.
In the next page, click on the big Build a Database button to create your database.
It will then give you three options to start with. Here, I am going for a Shared server, which is free. Notice that you also have the option of a Dedicated server, and you should use this for production of apps.
Now, it will ask you to choose a cloud provider and the region for the server. Choose the server that is nearest to your user base, as the lag will be minimal. Click on the Create Cluster button.
Next, it you will ask for a username and password. You should remember this, as you’ll need it to connect through the NodeJS application. After providing the username and password, click on the Create User button.
You also need to give the IP address and for your testing project. You should then click on the Add My Current IP Address button.
After that, scroll down a bit and click on the Finish and Close button.
On successful creation of the user and IP address, you will get this pop-up. Click on the Go to Databases button.
Now, you will be taken to below screen, which shows your cluster. Here, click on the Connect button.
A pop-up will appear. Click on the Connect your application option in the middle.
Now, you will get the connection string and you can copy it. You will need it to connect your NodeJS application next to the MongoDB database.
Connecting to Atlas
You will now connect a simple NodeJS app to your newly created Atlas database. You will create a simple app with NodeJS and express by first creating a folder and then changing to it.
Now, you will create an empty node app by giving the command npm init –y.
You will then install the mongoose and express package in it. Mongoose is a npm module, which is required to connect NodeJS app to a mongodb database. And express is used in the NodeJS app to make the programming much easier.
Now, create a file server.js in the same directory and add the below code in it. Here, you are first importing mongoose and express. After that, you will create an app variable and use express(). Next, you will use mongoose to connect to your newly created Atlas database cluster.
You also need to check if the connection is successful. You will use db.once to check whether you are connected to the database. If the connection is not successful, then the db.on() will run.
Finally, you will use app.listen() to listen on port 3000 for your application.
Now, when you. go to the terminal and run the command node server.js to run your NodeJS app on port 3000, you will get the message that it is connected successfully to the database.
It is an important topic for any production application. Once our application grows, so will the number of users and other records. You will need to scale the database accordingly. MongoDB is a database that is created to scale and is different from traditional Relational databases, which are very difficult to scale.
MongoDB has a lot of scaling options, and MongoDB Atlas has built in scaling. There are two types of Scaling possible through MongoDB. They are -
In this type of scaling, we increase the processing power of the server. Here, we increase the RAM and the processor speed. This type of scaling increases cost and is generally better suited for small to medium applications to achieve the desired speed. This type of scaling can also be done easily in relational databases.
In this type of scaling, additional servers share the load. We can also use low configuration servers while horizontal scaling. Normally, this is very difficult to achieve in relational databases, because we need to divide tables which have relations between them. But in NoSQL databases like MongoDB, it is easier to achieve because everything is stored as JSON objects in MongoDB and there is no relationship between tables.
Horizontal Scaling is achieved in MongoDB through Sharding and Replica Sets.
With Sharding, MongoDB divides the data into different sets and stores them on different servers. This method is very useful in applications in which a lot of data is written in the database or for organizations that have to work on large datasets, as we must write the data in only one server.
MongoDB Atlas does automatic sharding for us, but it can be configured further. Sharding is not useful in cases where we need a high availability of data, because in the case of failover, when we don’t have the data readily available.
As the name suggests, this is an instance where a replication of the data has been stored in multiple servers. With replication, the availability of data is always guaranteed, but it causes issues in writing. So, to write the same data, we have to do it at multiple places.
Now, we will deploy our Node.js app in Heroku and go through the process to store the configuration variable. For this, we will first remove the credentials of the database connection created earlier and move it to an .env file.
Next, we will install a package of dotenv to use this environment file.
Now, in our server.js file, we will first import the dotenv, and after that, use the environment variables.
Now, once we run the node server.js command from the command line, we will again connect successfully to the database.
Now, we will finally deploy our app to Heroku. After installing the Heroku cli, we need to run the command Heroku login from the command line. It will take us to a pop-up, where we must provide our Heroku credentials.
After that, we need to initiate a git repository, add to it, and commit to it.
Next, we will use the command heroku create <app-name> to create a new app.
Finally, we will push the app to Heroku with the git push command.
Now, the app will be published, but we need to go to the app on the Heroku site and open Settings. After that, in config variables, we must add our DB_USER and DB_PASSWORD variables.
Deploying MongoDB in Different Environments
In this article, we have learnt about the different ways to deploy MongoDB. We have learnt how to deploy a MongoDB app through MongoDB cloud based service of Atlas in detail. And we also created a simple Node.js app and connected it to Atlas. The best way to learn something new is to do it often. It may take time but you’re sure to master it with practice.
If you have any questions, do mention them in your comments.