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.
Tomasz Stanczak Apr 30 2018-11-11T00: 00Z
source share