What is MongoDB?

MongoDB is a NoSQL, cross-platform database system. This means it does not arrange data in tables like normal relational databases. It is written in C++ and data is stored in JSON-like documents making them flexible in any platform.

The latest version of MongoBD is version 5 released on July 13, 2021. The new version brought about fixing of bugs and issues associated with previous releases and introduced new features like;

  • Time series collection
  • New Aggregation Operators like $count, $rand.
  • Creating multiple partial indexes
  • Change events output
  • Introduces Snapshots

Before proceeding with this, It’s is my understanding that you should at least have the basic understanding of databases.

In this guide I will show you how to;

  • Install MongoDB on Windows 10,
  • Add MongoDB executable file to PATH under system variables
  • Install MongoDB shell (mongosh)
  • Create database using mongosh

Installing MongoDB 5.0 on Windows 10 / Windows 11

Go to the official website of MongoDB, Click on Products then select Community Server. Alternatively, you can go directly to the setup downloads page and pull the latest release.

A new page opens with a list of the servers available. Click on MongoDB Community Server

The tab drops down to show the installer file. Ensure that the version is the latest in our case version 5, the platform is Windows and the package is msi. Next up Click on Download which starts immediately

After the download, Locate the installer file from your download and double click on it to install, The installation wizard opens.

Continue with the installation prompts by accepting the terms and conditions. Choose the setup type. I would recommend you go with the default complete. Hence click on Complete

For the system configuration ensure to check the box for installing MongoDB as a service and use the recommended network service.

For this guide I will use MongoDB Shell (mongosh) hence will not require MongoDB Compass. Hence deselect the checkbox and proceed with the installation.

The setup proceeds to start the installation as below. Click Finish Once done.

Install MongoDB Shell (mongosh) on Windows 10 / Windows 11

Find the download setup of mongosh from Here

Ensure the settings are for windows then the installer package is msi

The download starts immediately. Once its done locate the installer from your downloads or wherever you have saved it and double click on it to open the setup Wizard.

Continue with the installation prompt. Proceed to the destination folder, You can change the folder or leave it as it is. If you are using your own computer as Admin. It is okay to leave it as it is. If you are using a shared desktop it would be prudent to change the settings.

Proceed with the installation then click on Finish to exit the setup wizard

Add MongoDB to system Variable PATH

We start by locating the MongoDB bin folder, Copy the path

Open the system variables by typing env on the windows search bar

It opens a new window as below. Highlight on the path option to select it then click on Edit.

It opens a new window as shown below, Click on New then paste the path of the bin folder and click OK to save changes.

Locate the Mongo Home folder form the installation folder as below and copy its path

Back on the previous window, Click on New to add the MongoDB home folder to the system variable

A new window opens, Add the path under variable name and name the path as you would prefer then click OK to save changes.

It shows the path has been created as below.

Creating Database with mongosh

We now want to create a database using mongosh. Remember we installed the mongosh above because it is not customized to come with the MongoDB installer like MongoDB Compass.

Open you Command prompt and type in mongosh

It opens with the test database

To view the databases available use the code show dbs.

Create Database

No we want to create a new database. Use the code use db. ‘db’ here is the name of the database you want to create. In my case I created a database named ‘newdb’.
When you use the code show dbs the new database does not show, why?. Mongo DB only adds the database to the list when you insert data into it.

Insert Data

For inserting data you use the code db.collection.insert(). The collection here is the name of the type of document you want.
In my case I used Employee as I would want to add an employee then I used the fields for name and staffID. You can use or more field, However you prefer.

When you use the code show dbs again, You will now be able to view your newly created database.

Drop Database

But first you have to ensure that the database you want to delete is the one you are currently using by typing db to see the current database in use.

To drop or delete the database you use the db.dropDatabase() code to do so.

And when you use show dbs again, The database is no longer there on the list.

Conclusion

We have installed MongoDB, mongosh which is the default shell for MongoDB, added the mongo bin folder to the system variable path and also added the mongoDB whole folder as a system variable.

So that is the basic stuff to do with Mongo DB. Being that Mongo DB is NoSQL type, The commands are pretty easy and straightforward. I would encourage you to dig further with the use of this tutorial.

More guides available on our website:

LEAVE A REPLY

Please enter your comment!
Please enter your name here