How To Install MongoDB on Windows

MongoDB is the most popular NoSQL database server and it can be used to store JSON documents. MongoDB is a document-oriented, open-source and platform-independent Database Management System (DBMS). This tutorial provides all the steps required to install and setup the MongoDB 4 Community Server on Windows 10. The steps should be the same for other versions of MongoDB including MongoDB 3 and Windows systems.

Download MongoDB

Open the MongoDB Download Page and download the most recent version of MongoDB for Windows as shown in Fig 1.

Fig 1

Click on the Download Button to start the download as highlighted in Fig 1.

Install MongoDB

In this step, we will install the MongoDB server using the MSI installer downloaded by us in the previous step. Double click the installer to start the installation. It will show the welcome screen as shown in Fig 1.

Fig 2

Click on the Next Button to continue with the installation. The next screen shows the License Agreement as shown in Fig 2.

Fig 3

Accept the License Agreement as shown in Fig 3 and click on the Next Button to continue with the installation. The next screen asks to choose MongoDB setup type as shown in Fig 4.

Fig 4

I have clicked on the Custom Button to view the available components as part of this installation process as shown in Fig 5.

Fig 5

We need at least Server and Client options to work with the server. You may omit the other components as listed in Fig 5. You can also change the installation location at this stage by clicking on the Browse... Button. Now click on the Next Button to continue the installation. The next screen provides options to configure MongoDB as system service as shown in Fig 6.

Fig 6

I have kept the default options and clicked on the Next Button. The next screen provides options to install MongoDB Compass as shown in Fig 7. MongoDB Compass is the official graphical user interface for MongoDB to manage the database graphically similar to how we use Workbench for MySQL.

Fig 7

I have kept the option enabled to install MongoDB Compass as shown in Fig 7. Now click on the Next Button to continue with the installation. The next screen shows the options to confirm the installation using the options selected by us in the previous steps as shown in Fig 8.

Fig 8

Now click on the Install Button to start the installation. It will ask for system permissions to start the installation. Allow it to make the necessary changes as part of the installation process. It will show the installation progress and also ask to update the services as shown in Fig 9.

Fig 9

It also needs a reboot after completing the installation as shown in Fig 9. Close all the other windows to preserve the work in progress. Now click on the OK Button to continue with the installation. It will show the installation success message after completing the installation as shown in Fig 10.

Fig 10

Click on the Finish Button to close the installer. It will show the windows to restart the system. Now complete the steps to install the MongoDB Compass before the system restart. The installer will also launch the MongoDB Compass and it will ask to accept the License Agreement of MongoDB Compass as shown in Fig 11.

Fig 11

Read the License Agreement, scroll down and click on the Agree Button to accept it. It will also provide options to configure Privacy Settings as shown in Fig 12.

Fig 12

Configure your Privacy Settings and click the Start Using Compass Button to apply the settings. It will show the default screen of MongoDB Compass as shown in Fig 13.

Fig 13

This will complete the installation and setup of MongoDB with Compass. It will also create the Desktop Icon to launch the MongoDB Compass from the desktop. Close the MongoDB Compass. Now we can restart the system as shown in Fig 14.

Fig 14

Now click on the Yes Button to restart the system.

System Path

We can add the MongoDB to system PATH to access it directly from the command prompt without navigating to the bin directory. You can follow the below-mentioned steps to do so.

Right Click -> My Computer(This PC) -> Properties -> Advanced System Settings

The above steps will open the Windows settings panel. Now click on Environment Variables, select Path under System Variables section and click on Edit. We need to add the path of installed MongoDB to system Path.

Remove the path of previously installed MongoDB. Now click on New Button and add the path to installing MongoDB bin which is E:\tools\db\mongodb\4.2\bin in my case. Press OK Button 3 times to close all the windows. This sets the MongoDB 4 on system environment variables to access the same from the console.

Now again open the console and test the MongoDB version as shown in Fig - MongoDB Version.

Fig - MongoDB Version

It also confirms that MongoDB is successfully installed on the system.

Setup Admin User

Open the command prompt and type mongo to connect the MongoDB server running as system service on the default port i.e. 27017. It will launch the MongoDB shell as shown in Fig 15.

# Launch MongoDB Shell

mongo

Fig 15

It shows the warning message indicating that the RBAC is not enabled. MongoDB provides options to create the Authentication Database to store user details and privileges across different databases. The same user having appropriate permissions can act on multiple databases using the Role-Based Access Control (RBAC). The RBAC provided by MongoDB governs access to a MongoDB system.

Now list the databases as shown below.

# List Databases

show databases



# Output

admin 0.000GB

config 0.000GB

local 0.000GB

We can see that MongoDB has already created three databases i.e. admin, config, and local. Now we will add the user admin with root role to the admin database using the commands as shown below.

# Switch to admin database

use admin



# Create the admin user with root role

db.createUser( { user: "admin", pwd: "<password>", roles: [ "root" ] } )



# Output

Successfully added user: { "user" : "admin", "roles" : [ "root" ] }

This is how we can add admin users to the admin database and assign a role to the user.

Getting Started With MongoDB

In this step, we will connect to the MongoDB Community Server using the MongoDB Compass using the admin user created by us in the previous step. Launch MongoDB Compass using the desktop icon created in the previous steps. It will show the welcome screen as shown in Fig 16.

Fig 16

Now configure the connection to connect with MongoDB as shown in Fig 17.

Fig 17

After configuring the connection, scroll down and click on the Connect Button. It will connect to the MongoDB server and shows the default databases as shown in Fig 18.

Fig 18

Now start creating your own database using the Create Database Button.

This is how we can install the latest MongoDB i.e. MongoDB 4 with MongoDB Compass. We have also created our first admin user assigned the pre-defined root role. In the last section, we have created a connection using MongoDB Compass to connect with the database server. Now we can access the same database from both MongoDB Shell and MongoDB Compass.