Connecting NodeJs App With MongoDB Atlas

Connecting NodeJs App With MongoDB Atlas
Written by Minaz UddinAugust 3, 2020
5 min read
Backend Development
23 VIEWS 8 LIKES 2 DISLIKES SHARE
8 LIKES 2 DISLIKES 23 VIEWS SHARE
Minaz Uddin

Web Developer(Intern) @ Wikiance

This Blog will help you connect your Nodejs application to the mongodb atlas which is a cloud database...

Introduction

This blog will teach you how to connect your Node.js application to the MongoDB Atlas. Well there are a couple of ways you can use mongodb. You can either install mongodb into your local machine or you can use use the cloud database which is the MongoDB Atlas.

Your computer must have enough RAM to support the application. I would suggest using Atlas for the beginners.So lets get started!

MongoDB and Mongoose

  • MongoDB is a document database with the scalability and flexibility.
  • MongoDB stores data in flexible, JSON-like documents.
  • Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js
  • It manages relationship between data and provide schema validation.
  • Mongoose also provides connection between the database and the server.

Signing Up With Atlas & Creating a Cluster

Go to the link  https://www.mongodb.com/cloud/atlas  and click on start free.
  1. After creating an account, you are provided with 512 MB of memory.
  2. The Atlas will guide you through step by step process and eventually you can create clusters.
  3. In the cluster creation process select the cloud provider and the region and fill in the cluster name.
  4. Once the cluster is created you can see a connect button inside the sandbox.
  5. Click on connect -> Connect your application
  6. Set the Driver to Node.js
  7. Add your connection string into your application code.

You will see something like this,

mongodb+srv://<username>:<password>@node-api-shop.5wrjk.mongodb.net/<dbname>?retryWrites=true&w=majority
  1. Now you have to replace the username with your username and password with your password.
  2. To be on safer side, always use the auto-generated password to prevent parsing errors.
  3. And change the <dbname> in the url to test or any name of your choice.

Using Mongoose to Connect

The first step is to install the mongoose framework,

$npm install mongoose

Import the mongoose package into your code,

const mongoose = require('mongoose'); //imports mongoose package
const url = 'mongodb+srv://JohnDoe:1234@node-api-shop.5wrjk.mongodb.net/<dbname>?retryWrites=true&w=majority'; 

Connect to the database,

mongoose.connect(uri, { useUnifiedTopology: true }, { useNewUrlParser: true });

unifiedTopology and useNewUrlParser are the configuration properties added to prevent deprecation warnings

And with that you are connected to the database.

To verify that add a callback function to the connect() method

mongoose.connect(uri, 
                  { useUnifiedTopology: true },
                  { useNewUrlParser: true },
                  function() { console.log('connected...') }
                );

So, when you are connected successfully, it logs as connected!

With that you have now linked your application to the database :)

mongodb atlas
mongoose.connect()
MongoParseError
MongoParseError: Uri does not have domain name, hostname and tld
23 VIEWS 8 LIKES 2 DISLIKES SHARE
8 LIKES 2 DISLIKES 23 VIEWS SHARE
Was this blog helpful?
You must be Logged in to comment
Code Block

3 Comments

sumit Jalan

sumit Jalan

Ridwan Oladimeji

"When I read your blogpost, I feel so much at peace. This is an excellent piece of writing Minaz. Well done"

Ridwan Oladimeji

Same here...I feel he is good at writing

Minaz Uddin
Web Developer(Intern) @ Wikiance
1 Blog Posts
0 Discussion Threads
Trending Categories
93
Software12
DevOps34
Frontend Development13
Backend Development13
Server Administration13
Linux Administration12
Data Center14
Sentry11
Terraform15
Ansible9
Docker13
Penetration Testing12
Kubernetes12
NGINX8
JenkinsX10
Jenkins18
SSL6
Ethical-Hacking10
Python8
NodeJs9
RedHat8
Github11
AngularJs15
Google Cloud Platform (GCP)6
SonarQube9
Amazon Web Service (AWS)2
VMware17
Blockchain6
Snipe-IT7