如何在 MongoDB 中建立多对多关系模型(对于 MySQL 用户)
我有 MySQL
背景,并且正在尝试了解 MongoDB
。特别是,我正在努力概念化如何以“Mongo 方式”对 n:n 关系进行建模。
对于此示例,假设我们有两个集合
:用户
和兴趣
。我们需要能够表示或查询数据中的几件事:
- 用户的兴趣
- 用户的兴趣评级,例如“喜欢”或“不喜欢”
- 具有给定兴趣的用户
- 每个评级的计数器(可以递增/递减)兴趣
- 兴趣名称
在MySQL
中,我会创建一个users_interests
表,该表根据用户ID和兴趣ID建立索引。对于计数器,我将为每种评级类型设置单独的列,并且每次用户对兴趣进行评级/取消评级时,都会进行一笔交易以确保计数永远不会错误。
我尝试阅读一些架构设计,但无济于事。
你能帮助迷失的灵魂找到出路吗?
I come from a MySQL
background and am trying to wrap my head around MongoDB
. In particular, I'm struggling to conceptualize how I should model n:n
relationships the "Mongo way."
For this example, let's say we have two collections
: users
and interests
. We need to be able to represent or query for several things in our data:
- User's interests
- User's rating of interest, e.g. "like" or "dislike"
- Users with a given interest
- Counter (which can be incremented/decremented) of each rating of the interest
- Interest name
In MySQL
, I would have created a users_interests
table indexed on both user IDs and interest IDs. For the counter, I would have had separate columns for each rating type, and each time a user rated/un-rated an interest, done a transaction to ensure that the counts were never false.
I've tried reading about some schema designs, but to no avail.
Can you help a lost soul find the way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很好的问题。让我首先概述一下 N:N 关系的工作原理,然后我将详细介绍您的每个要点。
N:N 在 MySQL 中,通常您有一个将用户和兴趣关联起来的数据透视表(user_interests 表)。在 mongo 中,你的做法有点不同。您仍然拥有用户和兴趣集合,但是现在,您在用户的兴趣下存储键列表。像这样的事情:
通过将您的兴趣存储在兴趣表上的列表中,您可以执行您需要的每项操作。如果您想执行查询,您可以根据兴趣表中的 ID 进行查询,然后使用 $in 修饰符。
现在,对于您的兴趣集合,我将执行以下操作:
当向用户文档添加兴趣时,计数变量将取决于您的评级的定义。如果您将评级存储在单独的区域(或逻辑中),那么您分配给它们的值将是您与感兴趣的 int 值相关的值。 IE:用户对其评分为 meh(其值为 1),那么您将在计数值上加 1。
希望这对您有所帮助,并且至少带来了一些关于如何构建它的其他想法!
祝你好运,记住 MONGO 很棒。
Great question. Let me first outline a bit of how the N:N relationship works then I'll go into detail on each of your bullet points.
N:N in MySQL normally you have your pivot table associating between your user and interests (user_interests table). In mongo you do this a bit differently. You still have a users and interest collection, however instead now, you store a list of keys under interests for a user. SO something like this:
By storing your interests in a list which is keyed off on your interest table, you can perform each of the actions you require. If you wanted to do a query you'd od it based on the ID which is in the interest table then do a query using the $in modifier.
Now for your interests collection I'd do the following:
When adding an interest to a users document, the count variable would then depend on the definition of your ratings. If you're storing your ratings in a separate area (or in logic), then the value you assigned to them would be what you relate then to the int value in interest. IE: User rates it meh (which has a value of 1) then you would add 1 to the count value.
Hopefully this is helpful and has at the very least brought about some other ideas on how to structure it!
Best of luck, remember MONGO IS AWESOME.
为了维护每个兴趣的评分的全局计数,您将需要一个单独且独立的集合,当用户执行兴趣的喜欢/不喜欢操作时,您可以在其中使用原子更新运算符更新(添加或减去)评分。
您可以将每个用户的兴趣存储为用户集合本身中的子文档数组。
该数据的 JSON 结构类似于:
现在您可以使用以下方式查询内部键:
这将返回具有特定兴趣的用户。
To maintain a global count of the ratings of each interest, you will need a separate and independent collection where you update (add or subtract) ratings using atomic update operators as and when like/dislike actions for interests are performed by users.
You can store each User's interest as an array of sub-documents within the User collection itself.
The JSON structure of this data would be something similar to:
Now you can query on the internal keys using:
This will return users who have a particular interest.