MongoDB规范化,外键和加入 [英] MongoDB normalization, foreign key and joining

查看:1420
本文介绍了MongoDB规范化,外键和加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在深入MongoDB几天之前,我想我会问一个很基本的问题,我是否应该深入研究它。我基本上没有nosql的经验。

Before I dive really deep into MongoDB for days, I thought I'd ask a pretty basic question as to whether I should dive into it at all or not. I have basically no experience with nosql.

我读了一些关于文档数据库的一些好处,我认为这个新的应用程序,他们将是真的很好。对许多类型的对象(很多m对m关系)和子类做收藏,评论等总是很麻烦 - 这是一种痛苦的处理。

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

我也有一个结构,这将是一个痛苦在SQL中定义,因为它是非常嵌套的,并转换为一个文档比15个不同的表好多了。

I also have a structure that will be a pain to define in SQL because it's extremely nested and translates to a document a lot better than 15 different tables.

但我困惑了几件事。


  1. 希望保持数据库标准化吗?我真的不想更新多个记录。这仍然是人们如何在MongoDB中处理数据库的设计?

  1. Is it desirable to keep your database normalized still? I really don't want to be updating multiple records. Is that still how people approach the design of the database in MongoDB?

当用户收藏一本书并且这个选择仍然存储在用户文档,但是这本书被删除了?没有外键,关系如何分离?我自己手动负责删除自己的所有链接?

What happens when a user favourites a book and this selection is still stored in a user document, but then the book is deleted? How does the relationship get detached without foreign keys? Am I manually responsible for deleting all of the links myself?

如果用户喜欢不再存在的图书,加入)?我必须在这里做任何容错吗?

What happens if a user favourited a book that no longer exists and I query it (some kind of join)? Do I have to do any fault-tolerance here?


推荐答案

MongoDB doesn不支持服务器端外键关系,也不鼓励规范化。如果可能,您应该在父对象中嵌入您的子对象,这将提高性能,使外键完全不必要。这说明它并不总是可能的,所以有一个特殊的结构,称为DBRef允许引用不同集合中的对象。这可能不那么快,因为DB必须进行额外的查询以读取对象,但允许种类的外键引用。

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.

仍然需要手动处理您的引用。只有在查找DBRef时,您才会看到它是否存在,DB不会通过所有文档来查找引用,并且如果引用的目标不再存在,则删除它们。但是我想删除这本书后删除所有的引用需要每个集合一个查询,没有更多,所以不是真的很难。

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.

如果你的模式更复杂,可能你应该选择一个关系数据库而不是nosql。

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

还有一本关于设计MongoDB数据库的书: MongoDB的文档设计

There is also a book about designing MongoDB databases: Document Design for MongoDB

UPDATE 上面的书不再提供,有相当多的其他人。我不会链接他们所有,因为这样的链接可能改变,一个简单的搜索亚马逊显示多个页面,所以它不应该是一个问题,找到一些。

UPDATE The book above is not available anymore, yet because of popularity of MongoDB there are quite a lot of others. I won't link them all, since such links are likely to change, a simple search on Amazon shows multiple pages so it shouldn't be a problem to find some.

有关更多详细信息和示例,请参阅手动参考和DBRefs 的MongoDB手册页

See the MongoDB manual page for 'Manual references' and DBRefs for further specifics and examples

这篇关于MongoDB规范化,外键和加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆