How to Connect Mongodb database in Express

In this blog we will see how to connect Mongodb database in express.

MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. Instead of rows & columns, MongoDB used collections & documents to store data. A collections consist of a set of documents & a document consists of key-value pairs which are the basic unit of data in MongoDB.

MongoDB is NoSQL database which is used to store huge amount of data without any table like in SQL insted it uses rows and columns to store the data.It uses Collections to store data basically collection is set of documents which are of key-value pairs.

We firstly download mongodb Community server fromĀ https://www.mongodb.com/try/download/community

Secondly we must download certain package which are listed below:

npm install express
npm install mongoose

To connect a Node.js application to MongoDB, we have to use a library calledĀ Mongoose.

const mongoose = require("mongoose");

Use the Mongoose connect method to establish the connection

mongoose.connect("mongodb://localhost:27017")

app.js

const express=require('express')
const mongoose = require('mongoose')
const mongourl='mongodb://localhost:27017'

const connectTOMongo=()=>{
    mongoose.connect(mongourl)
    console.log('Connected to mongodb Successfully');
}

connectTOMongo()

Output:

PS C:\Users\HP\Desktop\CODESPEEDY\CONNECT MONGODB> node app.js
Connected to mongodb Successfully

 

Leave a Reply

Your email address will not be published. Required fields are marked *