Hello everyone, today I am going to show you how to install mongoDB. Also, show you how to connect mongoDB to node server.

Install MongDB

First of all, you need to download MongoDB. So go to MongoDB web site.

download mongodb
Then choose your OS version and click the download button. It may take some time to complete download depends on your internet speed.

After download completion, click on it to install.

After complete some step, You will see an option to install MongoDB compass. Please see my video tutorial if you do not find that options. It comes with MongoDB version 3.6. By default, it will be selected. By installing MongoDB compass you can easily explore your database like others.

Check Mongo Command

After finish installation, open command prompt or terminal. Type

Sometimes the path of mongod.exe does not add automatically. That way you get a message like mongod is not recognized.

If it happens to you, go to C drive, then Program Files, then MongoDB, then server, then bin. Copy path from above.

Next open Edit System Environment Variables, Click on Environment Variables button. Then Select path. Click on edit. Add new path which you copied previously. Please confirm that you add a forward slash after bin. Then Ok, Ok, Ok.

From command prompt try again. If it does not work. Please close it.

Run command prompt again as administrator. Type mongod -v. See how it works. So You successfully install MongoDB on your machine.

Create Directory for MongoDB

Now go to the root of C drive. Create a folder data. You may create anywhere you want. Then type mongod –dbpath ../data.

By default, MongoDB runs on port 27017. You will see a message wait for connection on port 27017.

Inside data folder, you will see some files and folders. Here your MongoDB database will be created.

Now back to your code editor. Here I open the same project that was created from my previous mean stack tutorial. If you do not complete that tutorial you can download the source code from github.com. Source code before start GitHub link.

Connect MongoDB

Let’s connect MongoDB from your server.

Open your project & import mongoose. To use mongoose you need to install it.

Go to terminal and type npm install –save mongoose. Hit enter key.

Mongoose is the Object Relational Mapper for MongoDB like Entity Framework for MS SQL Server.

To manage different configuration settings for my project add a config.js file. Write below code in this file

Here I add module.exports so that I can import this configuration file into others.
Now add your MongoDB running server URL as you see.

27017 is port on my local machine on which my MongoDB server is running and nodeDB is my database name.

For MongoDB, it is not necessary to create a database first. It will be created by itself when You add something to the collection. The collection is similar to the table in SQL database.

Now back to the server.js file and import config.js file for database URL.

Then connect MongoDB with database URL.

Add connected event. This will be fired when your server successfully connects to the database. Inside this event add a log by which you can detect is database connected or not.

Also, add error event that will be fired if any error occurs during database communication. Add log inside this event.

Now run your server by nodemon or simply node server.js.

Go to the terminal. If everything is ok, then you will see the log which you add already.

Please confirm first, your MongoDB server is running before run your server.

Open your command prompt you will see a message one connection is open.

Explore Database by MongoDB Compass

You can also explore your MongoDB server by MongoDB Compass.

Set port 27017 then click on connect button. Before press connect button you need to run mongodb server.

mongodb compass

After successfully connect to mongo server.You will be able to explore database and collection under this.

See video tutorial for better understanding.


Source Code: github.com(after complete)
Youtube Playlist: MEAN Stack
Mean Stack Tutorial: Mean Stack(step by step)

Feel free to give your valuable feedback in comment section. Enough for today, see you soon.


developer · February 28, 2018 at 12:17 PM

great job!

Rishi Shukla · June 17, 2019 at 7:27 AM

It worked, in my case I had to use the url “mongodb://localhost/db_name” to connect, anyways was helpful

Leave a Reply

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