Connecting NodeJs App With MongoDB Atlas

Connecting NodeJs App With MongoDB Atlas
Techiio-author
Written by Minaz UddinAugust 3, 2020
5 min read
Backend Development
31 VIEWS 8 LIKES 2 DISLIKES SHARE
8 LIKES 2 DISLIKES 31 VIEWS SHARE
Techiio-author
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
31 VIEWS 8 LIKES 2 DISLIKES SHARE
8 LIKES 2 DISLIKES 31 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block

3 Comments

avatar
sumit Jalan

avatar
sumit Jalan

avatar
Ridwan Oladimeji

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

avatar
Ridwan Oladimeji

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

Techiio-author
Minaz Uddin
Web Developer(Intern) @ Wikiance
Techiio-follower
1 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software91
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance