The MongoDB protocol is a simple socket-based, request-response style protocol. Connection with the client and Database server happens through a regular TCP/IP socket.

MongoDB uses TCP as its transport layer protocol. The predefined default port for connection is 27017.

Connection URL:

       mongodb:// is the protocol definition
       localhost:27017 is the server we are connecting to
               localhost is the address
               27017 is the default port
       /myproject is the database we wish to connect to

List of default MongoDB ports

The following table lists the default TCP ports used by MongoDB

Default port Description
27017 The default port for mongod and mongos instances.
27018 The default port when running with --shardsvr runtime operation.
27019 The default port when running with --configsvr runtime operation.
28017 The default port for the web status page.

Checkout MongoDB Interview Questions

How to change MongoDB default port

You can also change the default MongoDB port from 27017 to any port of your choice (make sure that no other application is using that port).

Method 1

1. Start MongoDB server with – port argument
Open command prompt as administrator, navigate to MongoDB installation directory’s bin folder then type mongod --dbpath "C:Datadb" --port 20000
You will see waiting for a connection on port 20000

Subscribe to our youtube channel to get new updates..!

2. Open another instance of command prompt as administrator, navigate to MongoDB installation directory’s bin folder then type mongo --port 20000
The connection will be established.

Method 2

  1. Create a configuration file “mongod.conf”. The configuration file is in YAML format.
  2. Run with flag --config location to a configuration file. e.g. mongod --config /etc/mongod.conf

Example configuration file

 destination: file
  path: "/mongodb/log/mongod.log"
  logAppend: true
     enabled: true
  fork: true
  port: 20000
  enableLocalhostAuthBypass: false

Notice that port number changed to 20000.

On mongo shell type

db.runCommand({whatsmyuri : 1}).

It will show the IP and port number the server is listening to.

Checkout MongoDB Tutorial

Connect to MongoDB

Please follow the steps to connect to the MongoDB.

1. Go to the MongoDB installation directory and navigate to the bin folder. Launch mongod.exe. You can also do this from a command prompt. This is the MongoDB server. You must have a Datadb directory defined for MongoDB.

2. Launch mongo.exe. This is the mongo client. You can do this from a command prompt but launch another instance. Please use run as administrator for both. Mongo.exe will open the MongoDB shell.

3. MongoDB also provides drivers for different programming languages.

Explore MongoDB Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Example 1 JavaScript

var mongo = require('mongodb');
var MongoClient = mongo.MongoClient;   
MongoClient.connect('mongodb://'dbusername':'dbpassword'@'dbhost':'dbport'/'dbname,function(err, db)
       console.log('Mongo Connected....');

Checkout MongoDB Interview Questions

Example 2 C++

#include "mongo/client/dbclient.h"
void run() {
 mongo::DBClientConnection c;
int main()
   std::cout << "connected ok" << std::endl;
 } catch( const mongo::DBException &e )
   std::cout << "caught " << e.what() << std::endl;