Handling Data Using MongoDB

Khushi Sunil Bhatt
9 min readJun 21, 2021

Hey! In this fun blog, we shall learn all about what MongoDB is and how to get started with it as a first time user. It’s finally time to address the elephant in the room with our first question.

What even is MongoDB?

Before we begin with all the what is and how to’s of this topic, here’s a fun fact!

From where do you think the term ‘Mongo’ of MongoDB was derived? Mango? Mongoose? Mongering? No! The term Mongo was actually derived from the word ‘humongous’ which accurately describes the massive size of this database! MongoDB was meant to be used not only to store a huge amount of data, but also be able to work with this enormous amount of data in the most effective and efficient way possible.

Apart from its other qualifications, a short answer to the above question would be: MongoDB isessentially a NoSQL document database.

But what do all those words actually mean? Let us further expand on these terms individually.

> Database: A database is said to be a structured way to store as well as access data.

> NoSQL: A NoSQL database involves a method of storing data that does not use the standard approach of related tables of data. A more simplified way to put it would be data that is stored in an organized manner, but just not in the conventional format of rows and columns.

> Document: Akin to the word’s original meaning, data in MongoDB is stored in the form of documents, wherein a number of documents are grouped together to form a collection. We shall delve into the details of this topic in the coming sections.

Besides being a database, another functionality of MongoDB is that it is also a server. This makes running different databases on it possible.

So, How is Data Actually Stored?

Now, you may be wondering, “If all that data isn’t being stored in rows and columns, then in which other format is it being stored? And also, aren’t tables the most efficient way of retrieving data?”

To answer your question, let’s jump into what exactly a document is and what it looks like.

By definition, a document is a way to store and organize data as a set of field-value pairs.

In the given image, the text in red represents the ‘field’ name which is a unique identifier of the data it is holding. It works the function of a key.

The text in purple represents the ‘values’. A value is the data related to the given identifier.

Simple enough?

Let’s look at an example.

In the given example, the fields are name, gender and age. Notice how the field names are enclosed in quotation marks.

The values in this example are Sarah, Female and 23. An important thing to note here is that String values are enclosed in quotation marks, whereas the integer value of age is not.

NOTE: We can also store boolean values, lists and nested data in these documents.

This kind of structure enables us to create relations between different kinds of data and store them in the same document, which makes the retrieval of data a lot more efficient.

Case Comparison: For those of you who are familiar with MySQL, you may remember that in order to retrieve all the data related to a single object, we would have to perform complex joins on multiple tables. In a pleasant contrast to that, don’t you think this format of storing data in MongoDB is a lot easier?

Now that we have discussed what a document is, it is important to review some of the terms we mentioned earlier:

Collection

An organized store of documents in MongoDB, usually with common fields between the documents.

A collection would contain many documents similar to the example presented above, with common field names.

Database

A database would contain multiple collections.

We use a JSON (JavaScript Object Notation) data format to type out all our documents. MongoDB converts JSON data into a binary version (BSON) that can be stored and queried in a much more efficient manner.

A diagram depicting how MongoDB interacts with the server, browser and mobile app

Now that we’ve understood what MongoDB is along with some of its important structures and terms, we can finally move on to its installation so that we can actually use it! Take a deep breath. It’s not as hard as it seems. One…two…three…go!

Softwares To Be Installed

There are a couple of softwares that we are going to install to set up our environment.

Let’s start with the OSX Setup. Listed below are the softwares that we are going to be installing:

  • The Brew Package Manager: This package manager is used to install other pieces of software in libraries on our operating system.
  • Node: We will be using Node mostly for tooling of our applications and packaging. It shall be installed using the Brew package manager.
  • MongoDB: This shall be used as (duh) our database. The installation for this shall also be done through Brew package manager.
  • Lastly, we shall be downloading RoboMongo, a tool for browsing through the contents of our databases. This part is completely optional, although you may end up feeling like using it once you see how it actually works. :)

NOTE: If you use Mac or Windows, feel free to scroll directly to the part that mentions their respective steps.

OS Setup and Installation: Mac OS

If you are looking to create and deploy an Atlas cluster, click here. If this is your first time using MongoDB, don’t worry — keep reading.

Installing Brew

Okay, so let’s get Brewing! (pun intended)

Step 1: Open up a browser and navigate to “brew.sh” or just click here.

Step 2: Scroll a little bit down and look under the ‘Install Homebrew’ section.

Step 3: Select the link located under this section, and click on the clipboard icon located to the right of the link.

Step 3

Step 4: Open up a new Terminal window.

Step 5: Paste the link that you copied earlier directly into Terminal and hit Enter.

Step 6: The script will explain what it is going to do thereby giving you a chance to read it. It will pause before it performs the operation. Follow along with the prompts. It only takes a minute or two to install.

Installing Node

Step 1: Once your Brew package manager is installed, stay in the same terminal window and type: brew install node

This command translates to using the keyword “brew” to access the library package manager. We then mention the name of the package we want to install (in our case, node).

If you have your node version as anything before v12, I do recommend updating it. (To check which node version you have, just type “node -v”.) A simple way to do that if you already have node installed would be by just typing: brew upgrade node in the terminal window.

Step 2: Follow the prompts to install node.

Installing MongoDB

Step 1: Once you are done installing node, just continue in the same terminal window and type: brew tap mongodb/brew

This is the homebrew code to access MongoDB and its database tools. Once you click enter, you should be able to see something like this:

Step 2: Once that is done, wait for the prompt to appear for you to start typing. Now type: brew install mongodb-community

Step 3: Follow the prompts to complete the installation.

Starting up our MongoDB Server

The last thing to do now, would be to start up our server!

Open up a terminal window and type: brew services start mongodb-community

You should be able to see message saying — ‘Successfully started mongodb-community’!

Stopping our MongoDB Server

In order to stop our server, just type: brew services stop mongodb-community in the terminal window.

You should be able to see message saying — ‘Successfully stopped mongodb-community’.

OS Setup and Installation: Windows

If you are looking to create and deploy an Atlas cluster, click here. If this is your first time using MongoDB, don’t worry — keep reading.

Downloading the installer

Step 1: Start off by typing ‘mongodb community setup’ in a browser. Click on the first link that appears, or click HERE. (See picture below)

Step 1

Step 2: Click on the link that says ‘Install on Windows’.

Step 3: Scroll down to the section with the subheading ‘Procedure’ and click on the link that says ‘MongoDB Download Server’ or click HERE.

We are going to be installing the community version of the software.

Step 4: Make sure you have selected the tab that says ‘MongoDB Community Server’. On the right-hand side of the screen, you shall be able to see a drop down menu. Select the following options:

Step 4: Select the above options

Step 5: Click on the green ‘Download’ button.

Step 6: Make sure you know the location of where you are installing your ‘.msi’ file.

Running the MongoDB Installer

Step 1: Once the download is complete, open up your Windows Explorer or File Explorer.

Step 2: Navigate to the directory where you downloaded your MongoDB installer (which is your ‘.msi’ file). If you do not remember where you downloaded it, by default it should be in your ‘Downloads’ directory.

Step 3: Double-click on the ‘.msi’ file.

The steps to guide you through the installation wizard shall be discussed in the next section.

Operating the MongoDB Installation Wizard

Step 1: The first screen you shall see when you run the wizard will be ‘Choosing the Setup Type’. Here, you shall see two options:

  • Complete Setup: This is the setup type I would recommend using. This is because this setup option will install MongoDB along with its required tools to the default location of the folder.
  • Custom Setup: If you are particular about which executable you want to install and you also want to choose where to install them, this is the option that you should go for.

Give yourself a pat on the back. You’ve cleared the first hurdle. In our next blog post, we will be doing a small but fun activity/project in order to implement the MongoDB skills that we would have learnt by then.

If there are any questions that are still stuck in your head, feel free to throw them in comments section, and I shall try my best to reply to each of them.

Do connect with me via LinkedIn, and be sure to check out some of the fun yet simple projects that I post on my GitHub!

On that note, I wish you the best and hope you achieve all your mongoals (terrible pun, I know). Happy coding! :)

--

--

Khushi Sunil Bhatt

I write mostly for myself, sharing some of it with you :) A passionate software developer who is always open to learning new things.