,

Using Mongoose to Connect MongoDB with Node.js and Express.js

Posted by

Connect MongoDB with Node.js using mongoose

Connect MongoDB with Node.js using mongoose

In this tutorial, we will learn how to connect MongoDB with Node.js using Mongoose and Express.js.

MongoDB is a popular NoSQL database that stores data in a JSON-like format. Node.js is a runtime environment that allows you to run JavaScript code on the server-side. Mongoose is a MongoDB object modeling tool that provides a straight-forward and schema-based solution to model your application data.

Step 1: Install MongoDB

Firstly, you need to install MongoDB on your machine. You can download and install MongoDB from the official website: https://www.mongodb.com

Step 2: Install Node.js

Next, you need to install Node.js on your machine. You can download and install Node.js from the official website: https://nodejs.org

Step 3: Install Mongoose

Now, you need to install Mongoose in your Node.js project. You can do this by running the following command in your project directory:


npm install mongoose

Step 4: Create a Node.js project

Create a new Node.js project by running the following commands in your terminal:


mkdir myproject
cd myproject
npm init -y

Step 5: Connect MongoDB with Node.js using Mongoose

Now, create a new JavaScript file in your project directory and add the following code:


const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true })
.then(() => console.log('MongoDB connected'))
.catch(err => console.log(err));

This code will connect your Node.js project to a MongoDB database named ‘mydatabase’ running on localhost. Replace ‘mydatabase’ with the name of your database.

Step 6: Running your Node.js application

Finally, run your Node.js application by running the following command in your terminal:


node yourfile.js

Your application should now connect to the MongoDB database using Mongoose. Congratulations!

0 0 votes
Article Rating
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@josuedurandpebe9482
6 months ago

I am from Peru and thanks for your video it’s a Perfect video, thanks a lot!

@wajdefadool3459
6 months ago

thank you for the help

@recian_siya5378
6 months ago

Thank you sir ❤

@VGS_7
6 months ago

do a video so that we dont need mongo db compass … we connect it with only a nodejs file and send data using a node js file itself

@fam.agucci1237
6 months ago

Simplest is the best . your explaination was so Great!!! Arjun.👍

@QuanTran-wt4yt
6 months ago

hmm. You have only connected once so does it have any effect? I suggest that you should only connect when calling the api and close the connection when done. What do you think?

@rg_1971
6 months ago

This is such a great and easy to follow tutorial, thank you for sharing.

@thanonhari9643
6 months ago

Thank you Arjun, very intersting and helpful great!!!

@tahep3906
6 months ago

I've been looking for this kind of tutorial, most of the videos around on this subject are deprecated, thanks a lot and I'm looking forward to new ones