MongoDB is relational, you just need to rewire your brain

If you are new to MongoDB you might have to faced difficulties with relations. Which is due to the reason, MongoDB is documented as non-relational database which is misleading, We will explore why in the article, by directly comparing SQL relational query with MongoDB substitute query

Disclaimer: MySQL is a superset of SQL so below discussion will apply to all super superset of SQL

LEFT JOIN / LEFT OUTER JOIN

First, let’s understand Left Join in SQL

The LEFT JOIN keyword in SQL, returns all records from the left table, and the matched records from the right table, it returns all records from the left table, even if there are no matches in the right table

Equalization is used to know if two records have a common value

In SQL, the values of both rows are merged if two rows have the same equalization in MongoDB you provide an object key that will contain the other row

MongoDB has a well-documented alternative for Left Join, $lookup

$lookup is part of the Aggregate method

To understand lookup we need to first understand how aggregate works

Consider these documents part of purchases collection

Will give all the distinct values of products [‘toothbrush’,’pizza’,’ milk’,’guitar’]

It is used to narrow down the data set

It means, only does further aggregation with documents where the customer is mike or tom

It combines multiple documents into one document for reading purpose, keys inside $group define the new structure of the returned document

The above query add all the total values and output it expenditure key in the returned document

will sort the returned documents in descending order. -1 means descending and 1 means ascending (the default)

as: The field name, that will contain the data of inventory corresponding to the order

The output will be something like this

Official Documentation on Lookup

RIGHT JOIN

Right Join is just the opposite way of writing Left join, where Left table and Right table exchange places

So, obviously Right Join is possible in MongoDB, just have to exchange values between local and external values inside $lookup

INNER JOIN

It returns only common records

There is no direct analogy in MongoDB

You may be thinking `what if I just query all the documents from both collections with no limit and see which of them match?`

It will be expensive and slow when the number of Documents grows

Now you might be thinking

How the fuck SQL does it in milliseconds? Well, indexing. but as records will grow, it can take more than 5 seconds

To find documents with a common field value in both collections we can use $in

$in matches the field with all the array values given to it

So if we have a posts collection and pages liked collection

To find all the posts from pages you have liked, In SQL we would have done something like this

With MongoDB, we will have to make two queries like below and it will be faster than SQL

It is a good question, but it has drawbacks, MongoDB believes in the principle of writing code vertically than horizontally which is easier to manage while SQL believes in writing code horizontally which is inconvenient to debug and understand, I don’t like scrolling horizontally

MongoDB is not less powerful than SQL, you need to change your thinking which takes time

I prefer MongoDB for 3 reasons

  • You can scale it horizontally, It means you can add more servers instead of making one server more powerful.

This approach successfully prevents downtime. It is easy to scale, So it can manage huge data and that is in the name. “Mongo” being short for the word humongous which means huge

  • You can save JSON objects and query them

I can search the above document by

You can’t do this in SQL

  • You can change schema whenever you want. so it is easier to add new features

Now you know why developers love no-SQL database.

You will come across the idea of one collection, The premise of One collection is storing data as BSON in one Collection which was supposed to be stored in a different collection. This way you don’t have to care about relations because there is only one collection

Even Firebase (A BAAS service of Google) encourages this.

But Keep in mind it is not always great, you can quickly exceed the max document size and you may be able to extract what you want

As don’t do this

As you won’t be able to extract specific article, rather create a different collection for articles, then u can extract article individually as well as with users with $lookup just save username inside for articles

It is encouraged in Firebase because it doesn’t have any serverside join like $lookup you are supposed to do that on the client-side, you first load a document (let’s say docA) to load another document which is referenced by docA we need to load it the same way we loaded docA(well, firebase also doesn’t has the feature of making a field unique for that you will have to use its serverless function)

Leave a clap, It makes me feel less miserable writing, I live in India I don’t make a penny from these articles.

Best Of Luck, I appreciate you are still reading in 2020, You are gonna make it If you can survive this boring article you can battle anything. I believe in you.

Good Bye, for now, see you soon, also here are a few resources that helped me in the research

Resources: https://websitesetup.org/sql-cheat-sheet

https://www.mongodb.com/blog/post/joins-and-other-aggregation-enhancements-coming-in-mongodb-3-2-part-1-of-3-introduction

Finding Magic.