I'm surprised by this ability of MongoDB.
I'm surprised by this ability of MongoDB. In general, I prefer SQL databases, but this ability of MongoDB is amazing. I'm talking about nested data structures. In a SQL database, if you want to store an order with mul
I'm surprised by this ability of MongoDB.
In general, I prefer SQL databases, but this ability of MongoDB is amazing.
I'm talking about nested data structures.
In a SQL database, if you want to store an order with multiple items, you usually create an order record in the orders table.
Then, you create a separate order_items table and add multiple records depending on the number of items in the order.
After that, you create relationships between them.
But MongoDB has an interesting ability to handle this differently.
Because it allows you to store related data hierarchically inside a single document.
This prevents the need for extra joins, which are required in SQL databases.
Let's understand this with an example.
Suppose you purchase 5 items. First, you create one record in the orders table, then 5 separate records in the order_items table. To retrieve the information, you need to join these tables.
But in MongoDB, you can store all the related data in one document. When you need the information, you can retrieve that one document and get all the related information together, without needing an extra query or join.
But this doesn't work every time.
When you need only a small piece of information from a document, you might end up retrieving information that you don't need.
And of course, the choice depends on the use case.
What are your thoughts about this feature of MongoDB?
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.