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

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

Working on a local database is part of developing 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 sharded cluster database is where we store users’ data.

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

Prerequisites

Here are a few prerequisites to this guide:

If you are good to go, let us proceed.

Connect MongoDB using Mongo Client in Node.js

This section will help you to connect MongoDB on localhost 27017 using the Mongo client in Node.js. For this, we must install the mongodb NPM package.

Follow the steps below to connect to MongoDB using Mongo Client.

  • Navigate to a path in your system and create a project directory and move into the directory:
mkdir nodeMongo
cd nodeMongo
  • Now, initiate a Node.js project in that directory using the below command:
npm init -y
  • Create an entry point JavaScript file where we will write our code:
touch index.js
  • Install the required packages:
npm i express mongodb

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

  • Now, we will import the installed packages inside the index.js file, and create an express instance(app) and mongodb instance(MongoClient):
const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient;
  • Then use the below 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             
});

“mongodb://localhost:27017/myRandomDB” is a connection string to connect to the local database named myRandomDB.

  • Lastly, make sure you have set up a port for your application to listen to:
app.listen(3000, () => {
    console.log('Connected to PORT 3000...');
})

Note: Make sure that the MongoDB server is running in the background.

  • You can check for errors if any by running the application by executing the below command in the command prompt:
node index.js

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

Connect to MongoDB using Mongoose in Node.js

This section will help you to connect MongoDB on localhost 27017 using Mongoose in Node.js. For this, we must install the mongoose NPM package.

Follow the steps below to connect to MongoDB using Mongoose.

  • Navigate to a path in your system and create your project directory and move into the directory:
mkdir nodeMongooseMongo
cd nodeMongooseMongo
  • Now, initiate a Node.js project in that directory using the below command:
npm init -y
  • Create an entry point JavaScript file:
touch index.js
  • Install the required packages:
npm i express mongoose

The packages 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');

Now to connect to MongoDB on localhost 27017 using Mongoose use the below code snippet:

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...');
})
  • 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.

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. In layman’s terms, a local database is stored on the developer’s machine.

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 Mongoose. Hope you find it useful.

Reference

https://www.mongodb.com/blog/post/quick-start-nodejs-mongodb-how-to-get-connected-to-your-database

Aneesha S
Aneesha S
Articles: 172