MongoDB 数据库中的对象之间是否有可能存在关系?
这些天我在工作中使用 MongoDB。到目前为止,我觉得这是一次很棒的经历。
然而,我被要求在集合与 MongoDB 之间建立关系。这违背了 NoSQL 概念对我来说的目的,但作为这个领域的老菜鸟,我来征求其他意见。
例如,如果我们采用常见的角色/用户关系,是否可以有一个名为“角色”的引用集合并将一项的引用归因于用户项?
我开始考虑创建一个对象,为所请求的对象提供 ID,但感觉我不应该用 NoSQL 来做这些事情。
那么这里有人被要求做同样的事情吗?你成功了吗?如何成功?
I am using MongoDB at work these days. So far, I find it a great experience.
However I am asked to make relations between collections with MongoDB. This goes against the purpose of the NoSQL concept to me but being a good old noob in this area, I came to ask for other opinions.
For example if we take the common Role / User relationship, is it possible to have a reference collection named "Roles" and to attribute a reference of one item to a User item?
I started thinking about creating an object which provides an ID to the object requested but it feels like something I should not have to do with NoSQL.
So are there people here who have been ask to do the same thing? Did you succeed and how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MongoDB(以及大多数其他 NoSQL 数据库)本身并不支持关系的概念。 RDBMS 具有本机查询工具来定义和利用 MongoDB 所缺乏的关系(例如 JOIN)。
这并不意味着您无法在 NoSQL 数据库中定义关系/引用。它只是意味着没有关系感知的本机查询运算符。
在 MongoDB 中,可以通过三种不同的方式“引用”一个文档:
将引用文档的 ID(通常是 ObjectId)存储为引用文档中的字段。如果您的应用程序知道必须在哪个集合中查找引用的文档,那么这是最好的方法。
示例:{_id: ObjectId(...); userId: ObjectId(...) <- 引用)。
第二种方法是使用 DBRef 约定,它以形式化的方式定义对文档的引用:
{ $ref :
collname>, $id :[, $db :] }
。在几乎所有情况下,第一种方法是首选,但 DBRef 确实允许引用应用程序可能不知道其类型或位置的文档。更多信息请参见:http://www.mongodb.org/display/DOCS/Database+References# DatabaseReferences-DBRef 以及关于何时使用它们的好文章:http://valyagolev.net/article/mongo_dbref/技术上不是参考,但在很多情况下这是有意义的将(部分)文档嵌入到其他文档中。请注意,对于 NoSQL 数据库来说,架构的规范化应该不是重点。
示例:{_id: ObjectId(...); user: {_id: ObjectId(...), name:"Willy Wonka"}}
。尽管如此,您可以在 MongoDB 中使用完全规范化的模式,并且大多数 ORM 库将为您完成许多非 MongoDB 原生的工作。在大多数情况下,这确实意味着您最好使用传统的 RDBMS。如果您因为认为 MongoDB 是 MySQL 的快速版本而改用它,那么您就被误导了。两者都有功能性的最佳点,但重叠程度有限。如果您的应用程序严重依赖关系功能,请不要使用 NoSQL。
其他值得阅读的文章可以帮助您加快非关系思维的速度:
http://www.mongodb.org/display/DOCS/Schema+Design
MongoDB (and most other NoSQL databases) do not natively support the concept of relations. RDBMSs have native query tools to define and make use of relationships (JOINs, for example) that MongoDB lacks.
This doesn't mean you cannot define relationships/references in NoSQL databases. It simply means there are no native query operators that are relation aware.
There are three different ways to "refer" to one document from another in MongoDB :
Store the referred document's ID (usually an ObjectId) as a field in the referring document. This is the best approach if your app will know in which collection it has to look for the referred document.
Example : {_id: ObjectId(...); userId: ObjectId(...) <- reference).
The second approach is using the DBRef convention which defines a reference to a document in a formalized way :
{ $ref : <collname>, $id : <idvalue>[, $db : <dbname>] }
. In almost all cases the first approach is preferred but DBRef does allow references to document the application may not know the type or location of. Furhter information here : http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef and a good read on when to use them here : http://valyagolev.net/article/mongo_dbref/Not technically a reference but in a lot of cases it makes sense to embed (parts of) documents into other documents. Note that normalization of your schema should be less of a focus with NoSQL databases.
Example : {_id: ObjectId(...); user: {_id: ObjectId(...), name:"Willy Wonka"}}
.All that said, you can use a completely normalized schema in MongoDB and most ORM libraries will do a lot of the work for you that is not native to MongoDB. In most case this does mean you'd be better off with a traditional RDBMS though. If you're switching to MongoDB because you think it's a fast version of MySQL you have been misinformed. Both have functional sweetspots with only limited overlap. If your app relies heavily on relational functionality don't use NoSQL.
Other articles worth reading to get you up to speed on non-relational thinking :
http://www.mongodb.org/display/DOCS/Schema+Design
我会这样做:
您重复自己在 NoSQL 结构中不是问题。标准化并不是最佳选择。
或者,您可以将每个成员的 $_id 存储在“Users”数组中,并将用户及其姓名、电话等存储在单独的集合中。另一种选择是将用户文档存储在“Users”数组中,但这可能会导致管理起来很麻烦。
I would do it like this:
That you are repeating yourself is not a problem inside a NoSQL structure. Normalization is not the optimum for it.
Alternatively, you can just store the $_id of each member in the "Users" array and have the users with their names, phone,... in a seperate collection. Another option is to store user documents in the "Users" array, but this may get cumbersome to administrate.