mongodb - MongoDB中的外键关系

如何在 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/

相关文章:

java - 仅为一个字段编写 Mongo 转换器

javascript - mongodb如何动态创建数据库/集合

node.js - ReactJS:如何在本地服务器上部署

mongodb - 为什么 MongoDB 可以创建唯一索引而 Mongoid 不能?

python - Pymongo 或 Mongodb 将两个相等的 python 字典视为不同的对象

php - 如何在 Waterline 或 MongoDB 中选择、分组和加入

javascript - 使用 express 将 MongoDB 查询结果作为 JSON 发送

mongodb - 如何将 MongoDB 聚合用于通用集合操作(​​联合、交集、差异)

javascript - MEAN堆栈中的动态html表单生成

mongodb - 无法使用 Mongo Shell 连接到 MongoDB Atlas 集群