面向文档的数据库中的关系? [英] Relations in Document-oriented database?

查看:95
本文介绍了面向文档的数据库中的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对面向文档的数据库感兴趣,并且想与MongoDB一起玩.因此,我开始了一个相当简单的项目(一个问题跟踪器),但是却很难以一种非关系的方式进行思考.

我的问题:

  1. 我有两个相互关联的对象(例如issue = {code:"asdf-11", title:"asdf", reporter:{username:"qwer", role:"manager"}}-这里有一个与此问题相关的用户).我应该创建另一个文档用户"并按其ID在问题"文档中引用它(如在关系数据库中),还是应该将所有用户数据保留在子文档中?

  2. 如果我在文档中有对象(子文档),我可以在单个查询中更新它们吗?

解决方案

我对面向文档的数据库完全陌生,现在我正尝试使用node.js和mongodb开发某种CMS.我面临着与您相同的问题.

通过反复试验,我发现了一条经验法则:我为可能是查询的主题"的每个实体创建一个集合,同时将其余实体嵌入到其他对象中.

例如,可以嵌入博客条目中的评论,因为通常它们绑定到条目本身,并且我无法考虑对所有评论进行全局有用的查询.另一方面,附加在帖子上的标签可能值得拥有自己的收藏集,因为即使它们绑定到该帖子,您也可能希望全局地对所有标签进行推理(例如,列出趋势主题).

I'm interested in document-oriented databases, and I'd like to play with MongoDB. So I started a fairly simple project (an issue tracker), but am having hard times thinking in a non-relational way.

My problems:

  1. I have two objects that relate to each other (e.g. issue = {code:"asdf-11", title:"asdf", reporter:{username:"qwer", role:"manager"}} - here I have a user related to the issue). Should I create another document 'user' and reference it in 'issue' document by its id (like in relational databases), or should I leave all the user's data in the subdocument?

  2. If I have objects (subdocuments) in a document, can I update them all in a single query?

解决方案

I'm totally new to document-oriented databases, and right now I'm trying to develop sort of a CMS using node.js and mongodb so I'm facing the same problems as you.

By trial and error I found this rule of thumb: I make a collection for every entity that may be a "subject" for my queries, while embedding the rest inside other objects.

For example, comments in a blog entry can be embedded, because usually they're bound to the entry itself and I can't think about a useful query made globally on all comments. On the other side, tags attached to a post might deserve their own collection, because even if they're bound to the post, you might want to reason globally about all the tags (for example making a list of trending topics).

这篇关于面向文档的数据库中的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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