MongoDB Simplified Part 1: What, Why, and How?

This is the first article of series MongoDB Simplified which will cover all the basics of MongoDB. I will try to keep things as simple as possible.

We will start from understanding What is MongoDB?, How does it work?, Pros and Cons?, How to set up on your machine going all the way to learning how to perform basic CRUD operations. CRUD is short for Create, Read, Update and Delete if you’re wondering. So let’s start.

What is MongoDB?

MongoDB is a database that is based on document model. It is a non-relational type database.

Now, what are relational and non-relational databases? 🤔

Assume RDBMS (Relational Database Management System) like an “Excel Sheet” with Rows and Columns to save data in the form of tables. This table will have a unique ID to identify each row and where multiple fields are distributed along the column. These types of databases usually have relationships between them, hence the name “Relational Database” 💡.

Whereas MongoDB (DBMS) saves data in JSON-like documents inside a collection having no relationships with other documents hence they are called “Non-Relationship Database” types. Example of JSON like object is shown below 👇🏼:

{
  "id": "123",
  "name": "Shad Mirza",
  "hobbies": "Coding, Art, Writing"
}

How does it work?

A record in MongoDB is a document. Now, what’s a document? 🤔

A document is a data structure composed of field and value pairs. A MongoDB document is similar to JSON object (see above example) but uses a variant called BSON (Binary JSON) that accommodates more data types. These documents are inserted separately which are unaware of other documents. (Non-Relational type, remember?)

It means that records are not restricted to have the same number of columns (which is a must in RDBMS).

Example: A collection of Employees can have multiple documents of each employee with a different number of key-value pairs i.e. one employee can have one phone number while other can have two phone numbers and that is totally fine.

{
    "id": "101",
    "name": "Ramesh",
    "personalNumber": "9123456789"
},
{
    "id": "102",
    "name": "Suresh",
    "personalNumber": "9123456788",
    "workNumber": "8123456789",
}

Now suppose we are using a relational database, then we are bound to use the same number of columns for each data.

What it means in the current example is that we would have to add a workNumber column for all the employees regardless of whether they need this field or not. This will result in “Ramesh” having an empty value in workNumber column 😓.

Without the restrictions of columns, the developer can add documents however they need without worrying that a little change will break everything 🤩.

MongoDB allows you to structure data in a way that is efficient for computers to process and easily readable for humans providing a natural way of storing and processing data across the application.

MongoDB is a distributed database, which means it provides three fundamental features that developers have to implement themselves otherwise. That’s why it is so loved by the developer’s community. Those 3 features are:

Understand NoSQL and SQL

Countless wars ⚔️ have been fought over debating SQL vs NoSQL and answer still stays the same, “It depends ”. Let’s get to the basics. What’s the dedicated purpose of a database? 🤔

“Storing Data” right?. Now in this context, let’s understand what are the differences between SQL & NoSQL database and how they store data.

SQL

Example

Employee Table:

employee_idemployee_namenumberwork_numberaddress
1Shad99887766559876543210121
2Vamsi8877665544null122
3Girish7766554433null123

Address Table:

address_idcitycountrypincode
121VaranasiIndia221000
122DelhiIndia212345
123HubliIndia564635

🧐 Few things to notice in this example:

  1. The two tables are interconnected with the FOREIGN KEY in the address column. This key can be used as id to reference the address table.
  2. SQL follows a certain structure, hence the column work_number is required whether we need it (for a particular row) or not (look at the null value for the second and third-row).
  3. To read the information about an employee, we have to query the employee table and then address table or we have to JOIN these two first and the get the data.

NoSQL

Example

{
    "_id": "1",
    "employeeName": "Shad",
    "number": "9988776655",
    "workNumber": "9876543210",
    "address": {
        "city": "Varanasi",
        "country": "India",
        "pincode": "221000"
    },
},
{
    "_id": "2",
    "employeeName": "Vamsi",
    "number": "8877665544",
    "address": {
        "city": "Delhi",
        "country": "India",
        "pincode": "212345"
    },
},
{
    "_id": "3",
    "employeeName": "Girish",
    "number": "7766554433",
    "address": {
        "city": "hubli",
        "country": "India",
        "pincode": "564635"
    },
    "techStack": [
        {
            "_id": "565",
            "tech": "React",
            "experience": "3 Years",
        },
        {
            "_id": "867",
            "tech": "MobX",
            "experience": "2 Years",
        },
    ]
},

🧐 Few things to notice in this example:

  1. There is no relationship between different objects in a collection. We can start adding new key-value pairs as we want. (On adding a new column in SQL, we have to deal with all the rows previously added, they will be assigned null values for the new field added).
  2. The collection doesn’t need to contain a specific number of values. We don’t need workNumber in the second and third object so we don’t save it at all, no null values.
  3. We are eventually going to need all the user info at once (including address) and we can easily get it in a single API call by saving them together.
  4. Having a JSON like an object allows us to store complex structure without worrying too much. See the last record where we are storing “techStack” in an array of objects 😵. This kind of flexibility comes very handy when you’re trying to prototype something really quick.

Pros and Cons

MongoDB is not a replacement of Relational Database, it’s an alternative. Both have their advantages and disadvantages and we must know when to use what.

This is the time where we clear that It depends debate. Let’s go through the pros and cons to understand this better.

Pros 🥳

Cons 😓

That’s it for this article and I hope it was helpful. In the next part, we will learn how to setup MongoDB on our machine and start using it. Until then, take care 👋, wear mask and happy coding. 😋 Shad