您将如何在MongoDB中建模友谊关系? [英] How would you model a friendship relationship in MongoDB?

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

问题描述

我知道已经给出了很多建议,建议将一系列引用嵌入到其他用户中,但是所有此类忽略的答案是友谊是一种双向关系,当爱丽丝是鲍勃的朋友时,鲍勃是自动成为爱丽丝的朋友.我不是在模仿追随者.

I know a lot of answers have been given that advise embedding an array of references to other users, but what all answers of this sort neglect is that friendship is a two-way relationship and that when Alice is Bob's friend, Bob is automatically Alice's friend. I'm not modeling followers.

因此,我不想每次新用户进入系统时都保留两个引用.我需要一个考虑到这种关系的双向性质的模型.

So I don't want to keep two references every time a new user enters the system. I need a model that takes into account the two-way nature of the relationship.

我当时在考虑友谊边缘"的集合,其中每个文档都引用了两个用户.想知道是否有关于此的文献.

I was thinking a collection of Friendship 'edges' where each document holds a reference to the two users. Wanted to know if literature regarding this already exists out there.

推荐答案

MongoDB通常不适用于建模图形关系.有专门的图形数据库可以胜任这项任务.

MongoDB is generally not a good fit for modeling graph relations. There are specialized graph databases which excel at this task.

但是,当您不想添加其他数据库技术时,我建议创建一个新集合frienships,并将每个朋友关系建模为包含两个条目的数组的文档,每个条目都有一个对象,其中包含其中一个用户的缩写信息,您需要在朋友列表中显示一个条目:

However, when you do not want to add another database technology to the mix, I would recommend to create a new collection frienships and model each friend-relation as a document with an array of two entries, each of them an object with the abbreviated information about one of the users which you need to display an entry in your friend-lists:

{
    friendship: [
       {
            id:123,
            name: "Bob",
            avatar: "Bob.jpg"
       },
       {
            id:456,
            name: "Alice",
            avatar: "Alice.jpg"
       }
    ]
}

从友谊文档中的用户文档中复制信息的原因是为了避免再次查询用户集合,以获取用于显示用户朋友列表的所有数据. MongoDB 不能执行联接 只能在以下位置执行JOIN未分片的集合,因此您应避免将特定用例所需的数据分散到多个集合中,即使这意味着您需要创建冗余.否则,您需要一个接一个地执行多个查询,这会大大降低应用程序的响应时间.

The reason for duplicating information from the user documents in the friendship documents is to avoid a second query to the users collection to obtain all the data for displaying a users friend-list. MongoDB can not do JOINs can only perform JOINs on unsharded collections, so you should avoid spreading data you need for a specific use-case over multiple collections, even when it means that you create redundancies. Otherwise you would need to perform multiple queries one after another, which slows down the response time of your application significantly.

当您想获取用户123的朋友列表时,您将执行db.friendships.find({"friendship.id", 123})(在friendship.id上的索引将提高性能),然后接收文档列表,其中Bob是第一个或第二个朋友.

When you want to get the friend-list of user 123, you would perform a db.friendships.find({"friendship.id", 123}) (an index on friendship.id will improve performance) and then receive a list of documents where Bob is either the first or the second friend.

然后,您将迭代这些文档并输出不是用户123的数组条目的简短信息.

You would then iterate these documents and output the short info of the array-entry which is not user 123.

或者,您可以使用聚合管道过滤掉数据库上的Bob条目.使用上面的$ match查询,$ unwind友谊数组,然后$ match那些id不为123的文档.这将是一个折衷方案:您可以节省带宽,但要消耗数据库服务器上的CPU负载.

Alternatively you can filter out the Bob-entries on the database with an aggregation pipeline. Use the $match query above, $unwind the friendship-array and then $match those documents where id is not 123. This would be a trade-off: You conserve bandwidth at the expense of CPU load on the database server.

要查询友谊关系是否已存在,请使用:

To query if a friendship-relation already exists, use:

db.friendships.find( { $and: [ 
       { "friendship.id": 123 }, 
       { "friendship.id": 456 } 
    ] } ).count();

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

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