本文介绍了用作图形数据库以查找“朋友".的“朋友"在MongoDb中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究图形数据库,发现了neo4j,尽管这似乎很理想,但我也遇到过Mongodb.

I have been investigating a graph database and I have found neo4j and although this seems ideal I have also come across Mongodb.

Mongodb不是官方的图形数据库,但我想知道它是否可以用于我的场景.

Mongodb is not an official graph database but I wondered if it could be used for my scenario.

我正在编写一个应用程序,其中用户可以有朋友,而那些朋友可以有朋友等,这是社交网络的典型社交部分.

I am writing an application where users can have friends and those friends can have friends etc, the typical social part of a social network.

我想知道我的情况是否足以满足Mongodb的需求.实施起来真的很容易,或者我真的需要专注于REAL图形数据库吗?

I was wondering in my situation whether Mongodb might suffice. How easy would it be to implement or do I really need to focus on REAL graph databases?

我确实注意到foursquare正在使用Mongodb,因此我认为它支持其基础架构.

I do notice foursquare are using Mongodb so I presume it supports their infrastructure.

但是,例如,找到我的朋友中也有共同朋友的所有朋友会多么容易?

But how easy would it be to find all friends of my friends that also have friends in common, for example?

推荐答案

尽管并非不可能,但MongoDB不适用于这种情况.

Although it wouldn't be impossible, MongoDB would not be a good fit for this scenario.

原因是MongoDB不执行JOIN.如果需要跨多个文档的查询,则需要为每个文档进行单独的查询.

The reason is that MongoDB does not do JOINs. When you need a query which spans multiple documents, you need a separate query for each document.

在您的示例中,每个user文档都有一个数组,其中包含其朋友的_id.要找到也是UserB朋友的UserA朋友的所有朋友",则意味着您将:

In your example, each user document would have an array with the _id's of their friends. To find "all friends of the friends of UserA who are also friends of UserB" would mean that you would:

  1. 找到userA并获取他的friends-array
  2. 找到该数组中的所有用户并获取他们的好友数组
  3. 找到这些数组中所有在其好友数组中具有UserB的用户

这是您必须执行的三个查询.在每个这些查询之间,必须将结果集发送到应用程序,应用程序必须制定新的查询并将其发送回数据库.从第二个查询返回的结果集可能会很大,这意味着第三个查询可能需要一段时间.

These are three queries you have to perform. Between each of these queries, the result set has to be sent to the application, the application has to formulate a new query and send it back to the database. The result-set returned from the 2nd query can be quite large, which means that the 3rd query could take a while.

tl; dr:使用正确的工具进行作业.当您的数据是基于图的并且想要对其进行基于图的查询时,请使用图数据库.

tl;dr: Use the right tool for the job. When your data is graph-based and you want to do graph-based queries on it, use a graph database.

这篇关于用作图形数据库以查找“朋友".的“朋友"在MongoDb中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:20