How Does MongoDB or NoSQL Database work? (Relations)

How Does MongoDB or NoSQL Database work? (Relations)

Let’s now have a look at the MongoDB world or the NoSQL world, there are other NoSQL solutions to AWS, DynamoDB would be one.

For example, a serverless one, now I’ll have a look at MongoDB since it’s similar popular NoSQL database.

NoSQL Database or MongoDB structure

How Does MongoDB or NoSQL Database work? (Relations)

Now what is a NoSQL database or what is MongoDB? now the name MongoDB is simply stemming from the word “Humongous” because it’s built to store lots and lots of data in a very efficient way, something SQL solutions can struggle with but more on that later.

How does NoSQL or MongoDB work?

How Does MongoDB or NoSQL Database work? (Relations)

Well we have databases in the NoSQL world queue like a shop database,
but then in there we don’t have tables but so-called collections, but you could translate it with table.

Essentially, they may be the users and orders collection in our shop database, now in such a collection we have so-called documents, now look a bit different.

They are basically the rows in your table, you could say but not only do they look a bit like Json here.

The really interesting thing is that they don’t have to use the same schema, you can have multiple documents in one collection which have different fields.

Also Read: SQL Database structure | Schemas | Data Normalization

So you might have a different structure in there, there is no scheme implied on you, so you could have this document in a collection. But then also, this one where you got no age or this one where you got no name.

And it’s not just that you say the value is null, you can really have totally different documents in one at the same collection.

This of course has two downside of you not being sure if all the data adheres to your format, but on the other end you don’t want to have that format in the first place if you decide for such a solution.

Because the big advantage of course is that this is a super flexible solution, here you can add new data. Let’s say you’re building a new business, you’re building a new app and at some point you want to fetch more user data than you did in the past.

Not true shil one which would well kind of be bad for the existing users, but maybe useful data which you want to have for all new users. Now you can still store them all in the same collection.

NoSQL or MongoDB Relations

Because there is no schema employed and you can therefore be flexible regarding your data format, there also are no relations. Well at least kind of you can theoretically set up relations and do decorating manually.

But in general, there are no relations in a NoSQL world, you have your collections with your documents in there and whilst you can as I just said kind of relate multiple collections and that is possible it is done in reality.

NoSQL solutions, NoSQL databases like MongoDB rely way less on such relations instead the ideas that you put all the information in one place. So you might have your orders collection where each order does not only contain the user and product ID.

But it contains the key user data you need to work with the order something like the addressed email and the ID and the key product data you need like to price the name and the ID anything like that.

So, whatever you might need to display it on an order page and process it internally, you would put that into your collection and therefore you might still have an extra users collection and an extra products collection with more detailed data.

You don’t really need to query that connect data because you got the data you need in each collection which you might be querying from different points of your application and this is the idea have less relation merging going on have super fast and efficient queries there for it.

Of course, the disadvantages that you have some duplicates data if the product name changes you have to update it not only in the products collection but also in the orders collection.

But if you have an application where you have a lot of reads and not that many writes for products, for example then this would be a great set up still.

Leave a Reply

Your email address will not be published. Required fields are marked *