如何在 Mongo DB 中实现类似关系的外键?
最佳答案
你看这个:MongoDB normalization, foreign key and joining && 进一步 http://shop.oreilly.com/product/0636920018391.do ===> http://books.google.com/books/about/Document_Design_for_MongoDB.html?id=TbIHkgEACAAJ&redir_esc=y
MongoDB doesn't support server side foreign key relationships, normalization is also discouraged. You should embed your child object within parent objects if possible, this will increase performance and make foreign keys totally unnecessary. That said it is not always possible, so there is a special construct called DBRef which allows to reference objects in a different collection. This may be then not so speedy because DB has to make additional queries to read objects but allows for kind of foreign key reference.
Still you will have to handle your references manually. Only while looking up your DBRef you will see if it exists, the DB will not go through all the documents to look for the references and remove them if the target of the reference doesn't exist any more. But I think removing all the references after deleting the book would require a single query per collection, no more, so not that difficult really.
编辑更新
http://levycarneiro.com/tag/mongodb/
levycarneiro.com/tag/mongodb [quote] 因此,您创建了 4 个集合:客户、供应商、员工和联系人。您可以通过数据库引用将它们连接在一起。这就像一个外键。但是,这不是 mongoDB 做事的方式。表现将受到惩罚。 [取消引用]
https://stackoverflow.com/questions/9939022/