How to Connect MongoDB on Localhost 27017 in Windows – Starter’s Guide

Connect MongoDB on Localhost 27017 Featured Image

In this tutorial, I am going to demonstrate how to connect MongoDB on Localhost 27017 in Windows 10.

To connect your application with your local MongoDB database, you need to connect MongoDB on localhost 27017 port. This is the default port the MongoDB database server listens to the commands.

Working on a local database is part of the development of any web, desktop, or mobile application. A locally set up database helps you conduct trials and tests on your application with regards to the database. A local database, in layman’s terms, is one that is stored on the developer’s machine.

The actual cloud-based database or the cluster database is where we actually store the data of users. It is recommended to not touch that database unless needed.

This tutorial will guide you with simple ways to connect MongoDB on localhost 27017 using a Windows machine with the help of the mongo client and also with Mongoose.

Here are a few prerequisites to this guide:

If you are good to go, let us proceed.

How to Connect MongoDB on Localhost 27017 Using Mongo Client in Nodejs

The first part of the guide will help you install and connect MongoDB on localhost 27017 using the Mongo client in Nodejs. For this, we must install the MongoDB NPM package.

Follow the steps below to get started.

  • Navigate to a path in your systema and create your project directory and move into the directory:
mkdir nodeMongo
cd nodeMongo
  • Now, create your Nodejs project in this location like this:
npm init -y
  • Create an entry point JavaScript file:
touch index.js
  • Install the required packages:
npm i express mongodb

The package shall now be installed in the node_modules directory in your project directory.

  • Now, we will import the installed packages:
const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient;
  • Next, you must start your MongoDB server using the terminal. Let us assume the path of your MongoDB database looks as shown in the code below:
mongod -dbpath C:\MyRandomNodejsApp\MyMongoDB
  • Paste this code to connect MongoDB on localhost 27017:
MongoClient.connect("mongodb://localhost:27017/myRandomDB", function (err, db) {
     if(err) throw err;   
  // Use this space to pass MongoDB CRUD code here             
});
  • Lastly, make sure you have set up the port for your application to listen to:
app.listen(3000, () => {
    console.log('Connected to PORT 3000...');
})
  • You can check for errors if any by running your Nodejs application:
node index.js

We have successfully connected to localhost 27017 to the “myRandomDB” database.

How to Connect to MongoDB on Localhost 27017 Using Mongoose in Nodejs

The second part of the guide will help you install and connect MongoDB on localhost 27017 using the Mongoose ODM for MongoDB in Nodejs. For this, we must install the mongoose NPM package.

Follow the steps below to get started.

  • Navigate to a path in your systema and create your project directory and move into the directory:
mkdir nodeMongooseMongo
cd nodeMongooseMongo
  • Now, create your Nodejs project in this location like this:
npm init -y
  • Create an entry point JavaScript file:
touch index.js
  • Install the required packages:
npm i express mongoose

The package shall now be installed in the node_modules directory in your project directory.

  • Now, we will import the installed packages:
const express = require('express');
const app = express();
const mongoose = require('mongoose');
  • Let us now connect to MongoDB on localhost 27017 using Mongoose by using the below lines of code:
mongoose.connect('mongodb://localhost:27017/someRandomDB', { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => {
        console.log(`CONNECTED TO MONGO!`);
    })
    .catch((err) => {
        console.log(`OH NO! MONGO CONNECTION ERROR!`);
        console.log(err);
    })
  • Next, make sure you have set up the port for your application to listen to:
app.listen(3000, () => {
    console.log('Connected to PORT 3000...');
})
  • Start your MongoDB database server by executing the .bat file. This will automatically open the Mongo shell for you.
  • Lastly, run your application:
node index.js

This is what our console prints when successfully connected to the MongoDB local database server:

Connected to PORT 3000...
CONNECTED TO MONGO!

This way we have successfully learned how to connect MongoDB on localhost 27017 on a Windows machine using both the Mongo client as well as Mongoose ODM for MongoDB.

Conclusion

Working on a local database is part of the development of any web, desktop, or mobile application. A locally set up database helps you conduct trials and tests on your application with regards to the database. A local database, in layman’s terms, is one that is stored on the developer’s machine.

The actual cloud-based database or the cluster database is where we actually store the data of users. It is recommended to not touch that database unless needed.

This tutorial guides you with simple ways to connect MongoDB on localhost 27017 using a Windows machine with the help of the mongo client and also with Mongoose. Hope you find it useful!

Noteworthy References

TutorialsTeacher