Database Connection with MongoDB and  Mongoose(ODM) in Nodejs

Database Connection with MongoDB and Mongoose(ODM) in Nodejs

ยท

4 min read

In this blog, i will tell you that how to do database connection with mongoDB and mongoose(ODM). so, we can perform all our queries easily and can also get to know that how the queries are being performed in nodejs projects. In industrial projects, mostly we do not use mongo shell to perform query operation because we have to perform lot more queries with some parameter conditions. so, lets jump into it.

Prerequisite

Before writing code you should have nodejs installed in your system. if not installed then see my blog. Setup of nodejs

Database Connection with MongoDB

To connect the mongoDB database with nodejs using mongodb package we have to follow the steps :

Step 1 : Open Vscode and create nodejs project by typing command :

npm init -y

Step 2 : Install package express, mongodb by typing command :

npm install express mongodb

Step 3 : You can install nodemon package. Basically this package helps to reflect the changes continuously whatever we are writing in our javascript files. This package helps better in development phase.

  • i will tell you that --save-dev this command is used when we have to install package as dev-dependencies. The meaning of dev-dependencies is that this package will used just in development environment only.
npm install --save-dev nodemon

Step 4 : Now, if you want to use es6 modules import way then just write type : "module" in your package.json file and to run your file through command then just write "start" : "nodemon index.js" in scripts object. nodejs-code2.png

Step 5 : Create an index.js file and let's just setup our nodejs server with the help of express and write this code for database connection.

import express from "express";
import { MongoClient } from "mongodb";

/* Here, we initialize the express to access all the method which it provide. */
const app = express();

/* This is our mongoDB url, basically we give this url when we have to see our local 
mongo server in our system using mongo compass or mongo shell and in end i used 
name "ecommerce", it is database name which can be of anything of your choice. */
const MONGO_URI = "mongodb://localhost:27017/ecommerce";

/* This is the port where our nodejs server will run, you can write any port number as
 you want. */
const PORT = 8000;

/* Here i used IIFE(immediately invoked function) for database connection. By this
function when our server will start then instantly this function will call. */
(async function connectDb() {
  try {
    const client = new MongoClient(MONGO_URI);
    await client.connect();
    console.log("Database is connected");
  } catch (error) {
    console.log("error : ", error);
    console.log("Database is not connected");
  }
})();

/* This is the function to listen the server */
app.listen(PORT, () => {
  console.log(`server is listening or port ${PORT}`);
});

Step 6: Now type command in terminal

npm start

Step 7: if you want to see folder structure and the output how it will show the output then see below image. nodejs-code.jpg

Database Connection with Mongoose

To connect the mongoDB database with nodejs using mongoose we have to follow the steps :

Step 1 : Open Vscode and create nodejs project by typing command :

npm init -y

Step 2 : Install package express, mongoose by typing command :

npm install express mongoose

Step 3 : Create an index.js file and let's just setup our nodejs server with the help of express.

import express from "express";
import mongoose from "mongoose";

const app = express();
const PORT = 8000;

const MONGO_URI = "mongodb://localhost:27017/ecommerce";

(async function connectDb() {
  try {
    await mongoose.connect(MONGO_URI);
    console.log("Database is connected");
  } catch (error) {
    console.log("error : ", error);
    console.log("Database is not connected");
  }
})();

app.listen(PORT, () => {
  console.log(`server is listening or port ${PORT}`);
});

Step 4: if you want to see folder structure and the output how it will show the output then see below image. nodejs-code1.jpg

Thanks for reading, i will write more blogs on mongoDB where i will cover all the mongoDB queries from basic to advanced and if this blog get helpful to you then please like ๐Ÿ‘ and share...

ย