What is MongoDB ?

How to install and use it

Sahil More
7 min readAug 31, 2021

Let’s walk through the points we will be discussing about MongoDB…🚶

  1. Introduction
  2. Why is it used ?
  3. Installation
  4. Verifying the Installation
  5. Retrieve databases using Command Prompt and GUI

Introduction

MongoDB is a cross-platform (runs on multiple operating systems), document-oriented database management system (DBMS). MongoDB is also a NoSQL database, which means it does not use SQL to perform operations on a database.

MongoDB uses documents that are in JSON-like format, known as BSON, which is the binary encoding of JSON.

It’s developed as an open-source project by MongoDB Inc. under the Server Side Public License.

Node and MongoDB work very-well together, in part because Mongo uses a JavaScript engine built into the database since JavaScript is good at handling JSON objects.

Here are some of the constructs that make up the database structure:

  1. Database: The container that holds a set of collections.
  2. Collection: A set of documents. This is similar to a table in an SQL database. However, unlike an SQL database, a collection does not have a set structure or pre-configured data types.
  3. Documents: A JSON-like object. This is similar to a row in an SQL database. A JSON object may also contain child objects, an array, etc.
  4. _id: Mandatory unique field in every document. It separates one document from another, so we can identify each document independently. MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time

Why is it used ?

MongoDB’s document model is simple for developers to learn and use, while still providing all the capabilities needed to meet the most complex requirements at any scale.

  • The document model maps to the objects in your application code, making data easy to work with
  • Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data
  • MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use
  • MongoDB is free to use

Pros of Using MongoDB Over MySQL

  1. Speed and performance

The major benefit is when a large set of unstructured data is involved. As we can see in the below examples MongoDB takes 0.002 seconds to update student emails whereas MySQL takes 0.2491 seconds to execute the same task. This is just one example but you get my point right? 😃

Update Query in MongoDB and MySQL (source)

2. High Availability and Cloud Computing

Setting a set of servers that can act as Master-Slaves is easy and fast in MongoDB than MySQL. Besides, recovery from a cluster failure is instant, automatic and safe. For MySQL, there is no clear official solution for providing failover between master and slave in the event of a failure.

Cloud-based storage solutions require data to be smoothly spread across various server to scale up. MongoDB can load a high volume of data as compared to MySQL and with built-in sharding(method for distributing data across multiple machines), it is easy to partition and spread out data across multiple servers as a way of utilizing the cost-saving solution as per the cloud-based storage merits.

3. Schema Flexibility

With a flexible schema it is easy to develop and maintain a cleaner code. Besides, MongoDB provides the option of using a JSON validator in case you want to ensure some data integrity and consistency for your collection hence you can do some validation before insert or update of a document.

4. Embedding Feature

Unlike MySQL where you cannot embed data to a field, MongoDB offers a better embedding technique for related data. As much as you can do a JOIN for tables in MySQL, you may end up having so many tables with some being unnecessary especially if they don’t involve so many fields. In the case of MongoDB you can decide to embed data into a field for related data or reference from another collection if you expect the document grow in future beyond the JSON document size.

For example if we have data for users who we want to capture their addresses and some other information, in the case of MongoDB we can easily have a simple structure like

(source)

But in the case of MySQL we will have to make 2 tables with an id referencing in this case.

(source)

5. Security Model

MongoDB leverages the popular role-based access control model with a flexible set of permissions. Users are assigned to a role, and that role grants them specific permissions over data sets and database operations. All communication is encrypted with TLS, and it’s possible to write encrypted documents to MongoDB data collections using a master key which is never available to MongoDB, achieving encryption of data at rest.

6. User-Friendliness

MongoDB is an attractive option to developers. Its data storage philosophy is simple and immediately understandable to anybody with programming experience.

MongoDB stores data in collections with no enforced schema. This flexible approach to storing data makes it particularly suitable for developers who may not be database experts, yet want to use a database to support the development of their applications.

7. Flexibility

This is an easy one, and a hands down win for MongoDB. The schemaless design of MongoDB documents makes it extremely easy to build and enhance applications over time, without needing to run complex and expensive schema migration processes as you would with a relational database.

With MongoDB, there are more dynamic options for updating the schema of a collection, such as creating new fields based on an aggregation pipeline or updating nested array fields. This benefit is particularly important as databases grow in size. In contrast, larger MySQL databases are slower to migrate schemas and stored procedures that can be dependent on the updated schemas. MongoDB’s flexible design makes this much less of a concern.

Installation

  1. Head over to MongoDB Community Download Center
  2. Check the version suitable for your OS and download it.

3. Proceed with the installation after download is finished.

4. Keep these default settings as it is so that we can access MongoDB under it’s default service name.

5. If you are going to opt for a GUI of databases kindly tick “Install MongoDB Compass”. We will be discussing about it further in this article 🙌

6. That’s all your installation is complete 💛

7. Lastly, you need to go to the path where MongoDB is installed on your system. In my case it’s C:\Program Files\MongoDB\Server\5.0\bin .

You need to add this path to your Environment Variables so that you can access MongoDB globally on your system 😎

That’s pretty much it. You have installed MongoDB on your system. Now let’s check if everything is in its place. 🙌

For more information related to installation on different operating systems you can head over to more detailed steps here 😸

Verifying the Installation

To verify if it’s properly installed on your system please perform mongo command in your command prompt.

If you get these results then it’s working perfectly on your system. 💃

Retrieve databases using Command Prompt and GUI

Using Command Prompt

To check the databases on your system from command promp use the command show dbs

Using Graphical User Interface (GUI)

Open MongoDB Compass application which we had installed previously in this article.

Press on the connect command and it will automatically connect you to your local MongoDB databases. The result will be

As we can see the list of databases present on our local machine, we can say that we have successfully installed and verified the MongoDB installation 💃

For performing CRUD operations on MongoDB visit this article here.
Please let me know if you have any issues in the comments 📝

Hope this article was useful for you guys 😄
Do let me know if you want some other articles of this type here 📪
And…

If you want to buy me a coffee ☕️
Thank You ❤️

--

--

Sahil More

Founder SINFORMS @sinforms.com | A software engineer who writes about some interesting facts, you’ll find me @sahilmore.vercel.app