MongoDB normalization, foreign key and join

Before diving into MongoDB in deep times, I thought I was asking a fairly simple question about whether to dive into it at all or not. I have no experience with nosql.

I read a little about some of the benefits of document databases, and I think they will be really great for this new application. It is always a hassle to make favorites, comments, etc. For many types of objects (many m-to-m relationships) and subclasses, this is a kind of pain to deal with.

I also have a structure that will painfully define in SQL because it is extremely nested and translates to a document much better than 15 different tables.

But I am confused by several things.

  • Is it preferable that your database be normalized? I really don't want to update multiple records. Is this still how people approach database design in MongoDB?

  • What happens when the user's favorite books and this selection are still stored in the user document, but then the book is deleted? How are relationships separated without foreign keys? Am I personally responsible for removing all links?

  • What happens if a user uses a book that no longer exists and I request it (some kind of connection)? Should I do any fault tolerance here?

+61
database mongodb nosql foreign-keys normalization
Apr 30 '11 at 12:17
source share
2 answers

MongoDB does not support foreign key relationships on the server side, normalization is also not recommended. You should embed your child into the parent objects, if possible, this will increase performance and make unnecessary foreign keys. However, this is not always possible, therefore, there is a special construct called DBRef, which allows you to refer to objects in another collection. This may not be so fast, because the database has to make additional queries to read objects, but it allows you to use a link to a foreign key.

However, you will have to process your links manually. Just looking at your DBRef, you will see if it exists, the database will not go through all the documents to look for links and delete them if the link’s purpose no longer exists. But I think that deleting all the links after deleting the book will require a separate collection request, no more, so it’s not so difficult.

If your schema is more complex, then you should probably choose a relational database, not nosql.

There is also a book on MongoDB Database Development: Document Design for MongoDB

UPDATE The book above is not available, but because of the popularity of MongoDB there are quite a few of them. I will not link them all, as such links may change, a simple search on Amazon shows several pages, so there should not be a problem to find some.

For more information, see the MongoDB manual page for Manual Links and DBRefs for more information and examples.

+59
Apr 30 2018-11-11T00:
source share

Above, @TomaaszStanczak claims

MongoDB does not support foreign key relationships on the server side, normalization is also not recommended. You should insert your child if possible inside the parent objects, this will increase performance and make foreign keys completely unnecessary. However, this is not always possible ...

Normalization is not discouraged by Mongo. To be clear, we are talking about two fundamentally different types of relationships that two data objects can have. In one of one child, only the parent is owned. In this type of relationship, the Mongolian way is embedding.

In another class of relationships, two entities exist independently - they have independent lifetimes and relationships. Mongo wants this kind of relationship not to exist, and he is hardly silent on how to deal with it. Embedding is simply not a solution. Normalization is not discouraged or discouraged. Mongo just gives you two mechanisms to deal with it; Manual refs (similar to a key with a foreign key constraint connecting two tables) and DBRef (another, slightly more structured way to do the same). In this case, SQL databases are used.

+15
Jan 16 '15 at 9:34
source share



All Articles