如何在MongoDB中建模多对多关系(对于MySQL用户) [英] How to model many-to-many relationships in MongoDB (for a MySQL user)

查看:122
本文介绍了如何在MongoDB中建模多对多关系(对于MySQL用户)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自MySQL背景,正试图将自己的头缠在MongoDB上.尤其是,我正在努力概念化如何以蒙哥方式"为n:n关系建模.

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."

对于此示例,假设我们有两个collections:usersinterests.我们需要能够表示或查询数据中的几件事:

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:

  • 用户利益
  • 用户的兴趣等级,例如喜欢"或不喜欢"
  • 具有特定兴趣的用户
  • 每个兴趣等级的计数器(可以递增/递减)
  • 兴趣名称

MySQL中,我将创建一个同时在用户ID 兴趣ID上建立索引的users_interests表.对于计数器,我将为每种评级类型分别设置一列,并且每当用户对利息进行评级/未评级时,都要进行一次交易以确保计数永远不会为假.

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?

推荐答案

好问题.让我首先概述一下N:N关系的工作原理,然后我将详细介绍您的每个要点.

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通常,您需要将数据透视表关联到用户和兴趣(user_interests表)之间.在mongo中,您的操作有所不同.您仍然有一个用户和兴趣集合,但是现在,您在用户的兴趣下存储了一个键列表.像这样:

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:

User Collection {
      "name":"Josh",
      "user":"jsmith",
      "interests":[
           {
            "_id":12345,
            "rating":"like"
           },
           {..}..
      ]
}

通过将您的兴趣存储在兴趣表中列出的列表中,您可以执行所需的每个操作.如果要进行查询,请根据兴趣表中的ID进行查询,然后使用

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:

User Interest {
      "_id":objectId
      "label":"Swimming",
      "count":intValue
}

在向用户文档添加兴趣时,count变量将取决于等级的定义.如果将等级存储在单独的区域(或逻辑上),则分配给它们的值就是您要关联的值,然后才是您感兴趣的int值. IE:用户将其定为meh(值为1),然后将1加到计数值上.

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!

祝您好运,请记住MONGO很棒!

Best of luck, remember MONGO IS AWESOME.

这篇关于如何在MongoDB中建模多对多关系(对于MySQL用户)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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