MongoDB has the ability to have documents that include arrays of other documents. This solves many cases when you have relationships in relational databases.
When an invoice has several items, you do not put these items in a separate collection. You insert them as an array.
It makes me think about real examples of when to use NoSQL, and not when?
There are many different NoSQL databases, each of which is designed with different uses in mind. But you marked this question as MongoDB, so I assume you mean MongoDB in particular.
MongoDB has two main advantages over relational databases.
Firstly, it scales well.
When the database is too slow or too large, you can easily add more servers by creating a cluster or a set of replicas of several fragments. This almost does not work with most relational databases.
Secondly, it allows the use of heterogeneous data.
Imagine, for example, a product database in a computer store. What properties do the products have? All products have a price and a seller. But processors have a clock speed, hard drives and RAM chips have a capacity (and these capabilities are not comparable), monitors have a resolution, and so on. How could you create this in a relational database? You would either create a very long product property sheet or you would create a very wide and sparse product table with all the possible properties, but most of them were NULL for most products. Both solutions are not very elegant. But MongoDB can solve this a lot better, because it allows each document in the collection to have a different set of properties.
What can he not do?
Like a fairly new technology, there is not much literature about this. The software ecosystem around it is also not so good. The tools you can get for relational databases are often much more brilliant.
There are also some use cases MongoDB is not suitable for.
- MongoDB does not do JOINs. When your data is very relational and denormalized, it will be counterproductive, it may be a bad choice for your product. But you can take a look at graphical databases such as Neo4j, which are more relationship oriented than relational databases. Update 2016: MongoDB 3.2 now has rudimentary JOIN support with a $ search aggregation step , but it is still very limited in functionality compared to relational and graphical databases.
- MongoDB does not perform transactions. At least not complex transactions. Certain actions that affect only one document are guaranteed to be atomic, but once you touch more than one document, you cannot guarantee that no other request will occur between them and an inconsistent state will be found.
- MongoDB is not suitable for special reports. Its data mining capabilities are severely limited. Rather, the new aggregation features help, and MapReduce can also solve some surprisingly complex problems when you learn how to use it smart, but SQL is usually the best tool for such things.
By desorming data, you must solve all the same problems as relational databases ... But there are rules on how to normalize data with relational databases. Are there rules you can use to help them denormalize data to use NoSQL?
Relational databases have existed for about 40 years. Their theory is a well-studied topic in computer science. There are whole libraries of books written about the theory behind them. Currently, every imaginary corner frame has a step-by-step solution.
But NoSQL databases, on the other hand, are a fairly new technology. We are still figuring out best practices. The most common tips are: "Use your own head. Think about what queries are most often performed and optimize your data schema for them."
Any examples when you want to consider using a NoSQL solution in parallel with a relational database?
If possible, I would advise against using two different database technologies in the same product:
- Anyone who supports and supports the product should be familiar with both technologies.
- Troubleshooting is getting a lot harder.
- System administrators must maintain and update an additional database
- You have an additional error that can lead to downtime.
I would recommend only mixing database technologies when fulfilling your requirements without it, and not just difficult, but physically impossible. Otherwise, make a choice and stay with it.