MongoDB - Animated, beginner overview
Tags: mongodb, database
Published: 2024-02-04 11:50:52

I love MongoDB, and I'm not the only one. I will tell you why I and so many other developers love it. Of course, other options exist on the market, so we will also see what makes document stores, particularly MongoDB, so different.

If you prefer watching videos here is a link 😉

{% embed https://www.youtube.com/watch?v=rLDc7sQrXlM&t=1s&ab_channel=TechVision %}

## How easy is it?

To start with, MongoDB is free and open-source. The setup is straightforward; you can install and run it on your computer in a few minutes. You can use command lines or a programming language to interact with the database. They've got support for the most popular programming languages through drivers. For example, you can use JavaScript or Python.

![MongoDB Key value pair,JSON format and document collection animation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62xkmvrijfpolmb2hg70.gif)


MongoDB is a document store. So, what's a document? Picture a Restaurant bill. For each item you've ordered, there is the name of the item and the price. A document is very similar. It's a series of what we call key value Pairs. Documents representing the same type of data, like Restaurant bills, will be grouped in a collection. 

The good thing about documents in MongoDB is that they can be represented as JSON. JSON is a very popular data format. It's everywhere on the web, and many APIs expose their data in that format. When developers work with MongoDB, it feels natural and familiar. In short, MongoDB is very easy to pick up.

## What makes it different?

One key feature of MongoDB is that it's schema-less. You don't have to define the structure of your data before you start using it. You can start adding data to your database, and your data structure can evolve over time. This is great for developers because it means they can be very flexible and iterate quickly.

And, of course, MongoDB has all the features you would expect from a robust database, including indexing and advanced querying capabilities through its aggregation framework. It also offers unique features like geospatial queries, time series data management, change streams, and many more.

Ease of use and flexibility are great, but more is needed to convince entire teams and organizations to switch to a new technology. Modern applications tend to be data-intensive. So, how does MongoDB solve the scaling problem? 

## How does it scale?

A popular Mantra in MongoDB world is. "data that is accessed together should be stored together". This means that a document needs to be a self-contained unit of data. If you have an application that displays students and the classes they attend, you can store in the student document the student information of course and the classes they attend. 

In other databases, known as relational databases, strict data separation by type is heavily encouraged. Students, classes and the link between them would be stored separately. When the application needs to display students,  student and class data must be put back together. However, if you have everything stored in the same document, it makes it faster to query that data.
Because a document is a self-contained unit of data, it's easy to make copies of the same document across multiple servers. And if you have a lot of people trying to access your application, you can spread the load across those servers. That is called replication. 

Another option is to take French students and put them on one server, and take your English students and put them on another server. That is called sharding.

But you have another option: you could do both replication and sharding. If you have your French student on one server, you could still make copies of those French students across multiple other servers. This way of making copies of the same data across multiple servers is called horizontal scaling or scaling out.

![Database sharding and replication animation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9j3n2bvwswurl2qr3hr2.gif)


## Does it scale better than other databases?

MongoDB and document stores generally are very good at scaling horizontally. MongoDB is designed to be distributed and has built-in concepts like replication and sharding. In other databases, especially relational databases, scaling up by acquiring more powerful and larger-capacity machines is more common. However, vertical scaling comes with exponentially increasing costs. Doubling the performance doesn't mean doubling the price; it often quadruples it.

In contrast, horizontal scaling, where you add more servers, has a linear cost structure. You only pay for each extra server. On top of that, there is a physical limit on how big a single machine can get. On the other hand, with horizontal scaling, there is virtually no limit on how many servers you can get. Or at least the only limit is how much you can afford.

--- 

In conclusion if you have to remember anything from this post. 
- MongoDB is **easy to use**, 
- It's very **flexible** 
- It's **scalable**