使用MongoDB中的属性表示多对多关系的最佳模型 [英] Best Model for Representing Many to Many relationships with attributes in MongoDB

查看:149
本文介绍了使用MongoDB中的属性表示多对多关系的最佳模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表示具有属性的多对多关系的最mongo方式是什么?

What's the most 'mongo' way of representing many-to-many relationships that have attributes?

例如:

MYSQL表

=> firstName,lastName,...

电影 => 姓名,长度......

peopleMovies => movieId,personId,language,role

将人们嵌入电影......?

Embed people into movies...?

在MongoDB中我理解 denormalize并嵌入是好的,但我不想人嵌入电影中,它在逻辑上没有任何意义。因为人们不一定只属于电影。

In MongoDB I understand it's good to denormalize and embed but I don't want to embed people into movies, it just doesn't logically make any sense. Because people don't necessarily only have to belongs to movies.

人物电影将是两个独立的集合。
=>嵌入 [{movieId:12,personId:1,语言:英语,角色:主要}。 ..]

People and Movies will be two separate collections. People => embed [{movieId: 12, personId: 1, language: "English", role: "Main"} ...]

电影 =>嵌入 [{ movieId:12,personId:1,语言:英语,角色:主要} ...]

这个问题解决方案是当我们想要为特定的电影更新一个人的角色时,我们需要运行两个更新查询确保两个集合中的数据同步。

The issue with this solution is that when we want to update a person's role for a specific movie we'll need to run two update queries to ensure data is in sync in both collections.

我们也可以做更多关系类似的事情并最终得到三个集合

We can also do something much more relational like and end up with three collections

=> firstName,lastName,...
电影 => 名称,长度..
Castings => movieId,personId,language,role

People => firstName, lastName, ... Movies => name, length .. Castings => movieId, personId, language, role

这个问题是因为MongoDB中没有连接语句,所以需要 3个查询来自人 - >电影反之亦然。

The issue with this is that because of the lack of a join statement in MongoDB, it would take 3 queries to go from people -> movies and vice versa.

这是我的问题,在 MongoDB 中建模这样的东西的其他方法是什么?以更多 NoSQL 的方式。就所提供的解决方案而言,哪一个在mongo的性能和惯例方面是最好的。

Here is my question, what are some other ways to model something like this in MongoDB and in a more NoSQL way. In terms of the solutions provided, which one would be the best in terms of performance and convention in mongo.

推荐答案

在很多方面meteor的API鼓励平面关系文档,但MongoDB是一个非关系数据存储。遗憾的是,这种冲突是开发人员解决的一个练习。

In many ways meteor's API encourages flat relational documents, however MongoDB is a non-relational data store. This conflict is, unfortunately, left as an exercise for the developer to solve.

模式结构和连接的概念是一个在单个答案中涵盖的巨大主题,所以我将尝试尽可能简洁。

The notion of schema structure and joins is an enormous topic to cover within a single answer, so I will attempt to be as succinct as possible.

假设你有评论和发布数据。考虑如果您在帖子中嵌入评论会发生什么。

Assume you have comment and post data. Consider what would happen if you embedded comments within your posts.


  • DDP对文档进行操作。每次添加同一帖子中的新评论时,都会发送所有评论。

  • DDP operates on documents. All of the comments will be sent every time a new comment in the same post is added.

allow 拒绝规则对文档进行操作。期望相同的规则同时适用于帖子和评论可能是不合理的。

allow and deny rules operate on documents. It may be unreasonable to expect that the same rules apply simultaneously to both posts and comments.

出版物往往在收藏方面更有意义。在上面的场景中,我们无法轻松发布独立于其帖子的评论列表。

Publications tend to make more sense in terms of collections. In the above scenario, we could not easily publish a list of comments independent of their posts.

关系数据库存在的原因很充分。其中之一是避免第二个解决方案中固有的多重修改问题。

Relational databases exist for good reasons. One of them is to avoid the multiple modification problem inherent in your second solution.


  • MongoDB本身不支持连接,并且没有核心包来生成响应连接。

使用您的第三个解决方案。根据我的经验,选择关系模型的原因远远超过数据存储所施加的限制。当然,克服缺少连接并不容易,但痛苦很可能只与少数发布功能隔离开来。以下是我强烈推荐的一些资源:

Use your third solution. In my experience, the reasons for choosing a relational model far outweigh the restrictions imposed by the data store. Of course overcoming the lack of joins isn't easy, but the pain is likely to be isolated to only a handful of publish functions. Here are some resources I'd highly recommend:

  • How to publish a many-to-many relationship on EventedMind. Chris covers your exact use case in detail, however he manually does the reactive join with observe callbacks, which I don't recommend.

的https://www.discovermeteor.com/blog/reactive-joins-in-meteor/\">反应加入流星发现流星百科全书。这包括了如何以及为什么应该进行反应性连接的基础知识。

Reactive joins in meteor from the Discover Meteor Encyclopedia. This covers the basics of how and why one should do a reactive join.

来自发现流星。这涵盖了我上面提到的许多要点,并讨论了何时以及如何对一些数据进行非规范化。

The denormalization chapter from Discover Meteor. This covers many of the points I made above and also talks about when and how to denormalize some of your data.

您可以使用发布关系以加入您的数据。替代方案包括:智能发布发布复合简单发布

You can use Publish with relations to join your data. Alternative packages include: smart publish, publish composite, and simple publish.

如果您需要更多信息,请在下面评论,我会更新我的答案。

If you need more information beyond this, please comment below and I will update my answer.

这篇关于使用MongoDB中的属性表示多对多关系的最佳模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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