Connecting Node.js To MongoDB

Connecting Node.js To MongoDB

MongoDB is one of the most popular databases. in the database field

It is document-oriented, with No SQL database. It stores the data in BSON format similar to JSON format (object-like format key: value pair) It has good scalability, and flexibility, and is easy to use. It is widely in backend web technologies and is the leading NoSQL database.

Let's understand how to connect MongoDB with Node js. Let's deep dive into the backend code. We will be connected to cloud MongoDB so no need to install the MongoDB server. Simple🤗

Requirement.

Basic understanding of Nodejs.

and Lots of excitement🤓

Step 1

Sign up to MongoDB

For this, you have to check out the link official website

Step 2

Go to New Project icon. This redirects you to another page where you have to do some GUI configuration.

Step 3

For example, I use DemoDB for the demo purpose

click to Next

Step 4

You can add members and set permissions. I skip this because this is a demo example

click to Create Project

Step 5

Now you have to Do some cloud setting.

click to +Create

Step 6

Now I am not going to say anything now it depends on you which one choose. I go with the free😅

Also, choose your Provider and region

click to the Create

Step 7

Here you are providing the username and password for login Remember the thing you have to preserve the password or key because without this you can't connect with your database so write it down in system storage. For further use.

click to Create User

Step 7

set up with My Local Environment

Add entries to your IP Access List for this I am using my current IP

Click to Finish and close

click to Go to Overview

Step 8

now click to connect this redirects you on this page

Finally, we reach that level where we are going to collect the connection string

click on Drivers

Copy the connection string. and close it go to Overview->Database

Step 9🫡

Let's jump into the coding part

Step 9.1

Create a local server. for use

npm init

Do just enter. It will set all the things automatically If you want to add details and stuff you can add them.

This creates a package.json file

Install the express framework and mongoose. Express here helps us in creating our app and Mongoose helps us connect to the MongoDB database.

npm install express mongoose --save

const expres = require ('express');
const mongoose = require('mongoose');

const app = expres();
const port = 8000;

mongoose.connect("mongodb+srv://username:<password>@cluster0.ziew2u4.mongodb.net/?retryWrites=true&w=majority")
.then((e)=> console.log("MongoDB Connected"));

app.listen(port,()=>{
    console.log(`Server is runing on port ${port}`);
})

Remember that you have to add your username, and password which were given by MongoDB otherwise, it will not connect.

mongoose. connect will take the connection string to connect the database.

If you want to handle the error with this function can write this code

 mongoose.connect("Connection Sting", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
})
  .then(() => {
    console.log('Connected to the database');
  })
  .catch((error) => {
    console.error('Failed to connect to the database', error);
  });

This is better than the above code.

Check out this code on GitHub🤠

At the MongoDB page, a connection is detected. If you provide the right connection string

You explore a lot more about your Database and collections on an official document

Thank you for reading my content. Be sure to follow and comment on what you want me to write about next 🤓

Did you find this article valuable?

Support Saurabh verma by becoming a sponsor. Any amount is appreciated!