In this tutorial, I will show you how to install MongoDB on a windows operating system.
MongoDB is a free, open-source, No SQL database. It is a document database used mainly as an alternative to relational databases like MySQL. It is widely used in development.
The advantage of using Mongo BD is that it is simple to use. In addition, the database stores unstructured data, making creating, updating, and deleting fields easy. Another advantage is that it works well with huge data volumes and is scalable.
When in development, it is easier to have MongoDB installed locally instead of running it from the cloud. Let's see how we can achieve this.
MongoDB installation
-
The first step is to head over to the MongoDB website by typing MongoDB on your browser. Click on the first link.
-
Once on the website, under the products section, find the community server and click on that. The site will automatically detect your operating system. Or click on this link instead.
-
Confirm the version of MongoDB you want to install (preferably the latest version).
-
Change the package type to MSI. You can choose a zip file if that is what you prefer.
Click on the download button to save the installer on your computer.
-
Once the file is downloaded, double-click on the installer, which takes you through the installation wizard.
-
Click on next, Accept the agreement, then choose the setup type as "complete installation," which offers some extra features.
-
Leave the service configuration on default and click next. Confirm it is set on "Run Service as Network Service user."
-
The next page asks you to install MongoDB compass (the graphical user interface for MongoDB). Accept and click on install.
This will install MongoDB on your system.
Mongo Shell Installation
The Server is not enough for your development. You need the shell to help you interact with your database. The Mongo shell is used in place of the MongoDB compass, where you will create, read, update, and delete fields.
Before updates, the installation of MongoDB installed the mongo shell as well. After updates, they are installed separately.
To install the Mongo Shell:
-
Head to the MongoDB website. Under the tool section on the products tab, find shell. You can follow the following link instead. ( https://www.mongodb.com/products/shell ).
This will take you to the page where you can download the MongoDB shell.
-
Choose the version you want and the platform, and download the zip file.
-
Once the file is saved on your PC, extract the files to your desktop.
-
Rename the extracted folder to:
MongoShell
You can skip this step if you wish. It just shortens the long name on the folder.
-
The next step is to cut this folder from your desktop to the folder where MongoDB Server was installed.
C:\Program Files\MongoDB
Paste it into this folder, and you are done.
With MongoDB and Mongo shell installed, we can start the Server.
Starting MongoDB Server
-
Navigate to the MongoDB installation folder and run it with the executable "mongod." You can do the same using the terminal. Open your terminal and navigate to the MongoDB installation folder.
cd C:\Program Files\MongoDB\Server\6.0\bin
-
Once you are in that directory, run the following command to start the Server.
mongod
After running
mongod
, you will notice that the Server shuts itself down. The reason behind this is there is a path directory missing from your computer. -
The error message shows that we must create those directories to solve this problem.
Open a separate terminal and navigate to the C directory or copy and paste the command below on your terminal and press enter.
cd C:\
In this directory, we have to create a folder called "data" and another folder inside data called "db"
mkdir data\db
With the folders created, we can run
mongod
again and see if we get the error. -
Go back to your first terminal and run
mongod
. This time around, The Mongo Demon should start without any errors.
Adding MongoDB to the System Path
You will notice that it is hectic to keep navigating to the MongoDB installation folder to run the mongod
command. So, we will add the MongoDB installation path to the environment variables to solve this issue. This will enable us to run the mongod
command from any directory on the computer.
To do this,
-
Navigate to the MongoDB installation folder and copy the path.
C:\Program Files\MongoDB\Server\6.0\bin
-
Open the start menu, search for environment variables, and click "edit system environment variables."
The window below will open.
-
Click on Environment Variables.
-
Under System variables, click on "path" and then Edit.
-
A new window opens. Click new and paste the MongoDB installation path here. Click Okay and exit all the windows.
If you followed the steps correctly, you should be able to run the command
mongod
from any directory on your computer.
Adding MongoDB to the System Path.
The process for adding MongoDB to the System environment variables is the same as above. The only difference is the installation folder changes.
C:\Program Files\MongoDB\MongoShell\bin
This is the path we will paste into the environment variables. That is if you followed every step as it is in this article.
Open the environment variables and paste the mongo shell path to the system variables path, and you are done.
Starting The Mongo Shell
With the Mongo Server started, you can start the mongo shell by running this command in the terminal.
mongosh
And with that, you are all set to start creating databases.
Ending
I hope this article was helpful. If it was, please share it with others, who might be interested.
Queries?? Drop them in the comments section.
Thank you for reading!!