How to use a MongoDB Atlas database in your Node.js app
Posted on February 10th 2019 by ZarkoHow to use a MongoDB Atlas database in your Node.js app
You can sign up for MongoDB Atlas on mlab.com. After initially registering you can login and create your database server (cluster), which will work similarly to the original one from mLab, after we set it up.
1) In the Create New Cluster screen, the free options to create your database server should be picked by default, so you should be able click the 'Create Cluster' button on the bottom left to proceed:
So, make sure that the 'FREE TIER AVAILABLE' options are selected and that it says FREE on the bottom-left corner, then click the green button to create your cluster.
2) After that, you will be taken to your database Clusters dashboard, where you may need to wait for a bit until the cluster gets created.
When it's done, you can click the CONNECT button for Cluster0 that just got created:
3)
- Click one of the buttons to add an IP address button and paste the following value: 0.0.0.0/0
After that, click the green 'Add IP Address' button. That will enable access to our database server from any IP.
- Create your MongoDB database user by entering a new username and password in the fields below. Remember your credentials and then press the 'Create MongoDB User' button.
After that is done, you can press the 'Choose a connection method' button on the bottom right of the dialog.
4) In the window that follows, click the 'Connect Your Application' button:
5) In the next window, click 'Short SRV connection string' and then the COPY button:
As pointed out in the screenshot, you need to replace <PASSWORD> with the password that you specified in the previous step.
6)
Paste the copied value to the mongoose.connect() string in your app, and place your password as instructed above.
So, the mongoose.connect() line in your Node.js application (app.js) would look something like this:
mongoose.connect("mongodb+srv://myusername:mypassword123@cluster0-uhi3c.mongodb.net/test?retryWrites=true");
* You can change test in the connection string to your database name
That's it! Your app.js should connect successfully to the MongoDB Atlas database, and you should be able to use it just like the original mLab database.
MongoDB Atlas documentation on how to connect it to Node.js: https://docs.atlas.mongodb.com/driver-connection/#node-js-driver-example
CONNECTING TO A MONGODB ATLAS DATABASE FROM THE MONGO SHELL:
1) Go back to the previous window to choose a connect method (you can click the CONNECT button for your Cluster0 database, as shown above):
Choose the 'Connect with the Mongo Shell' option.
2) In the next window, select your operating system. I chose Ubuntu for this example. If the mongo shell isn't set up for your terminal, follow the instructions shown on their website.
After you COPY the mongo shell command, go to your terminal and paste it there. After you press Enter, you will be prompted to type your database user password (it won't show feedback as you are typing it, but it is getting registered). After you type your password, press Enter again. If the credentials are correct, you should be connected to your Atlas database via the mongo shell.