database 在 mongodb 中存储图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5125709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:05:03  来源:igfitidea点击:

Storing a graph in mongodb

mongodbgraphdatabase

提问by kefeizhou

I have an undirected graph where each node contains an array. Data can be added/deleted from the array. What's the best way to store this in Mongodb and be able to do this query effectively: given node A, select all the data contained in the adjacent nodes of A.

我有一个无向图,其中每个节点都包含一个数组。可以从阵列中添加/删除数据。将其存储在 Mongodb 中并能够有效执行此查询的最佳方法是什么:给定节点 A,选择 A 的相邻节点中包含的所有数据。

In relational DB, you can create a table representing the edges and another table for storing the data in each node this so.

在关系数据库中,您可以创建一个表示边的表和另一个表来存储每个节点中的数据。

table 1 
NodeA, NodeB
NodeA, NodeC

table 2 
NodeA, item1
NodeA, item2
NodeB, item3 

And then you join the tables when you query for the data in adjacent nodes. But join is not possible in MongoDB, so what's the best way to setup this database and efficiently query for data in adjacent nodes (favoring performance slightly over space).

然后在查询相邻节点中的数据时加入表。但是在 MongoDB 中无法连接,那么设置此数据库并有效查询相邻节点中的数据的最佳方法是什么(在空间上略微有利于性能)。

采纳答案by Chuck

I'm picking up mongo, looking into this sort of schema as well (undirected graphs, querying for information from neighbors) I think the way that I favor so far looks something like this:

我正在学习 mongo,也在研究这种模式(无向图,从邻居那里查询信息)我认为到目前为止我喜欢的方式是这样的:

Each node contains an array of neighbor keys, like so.

每个节点都包含一组邻居键,就像这样。

{
 nodeIndex: 4
 myData: "data"
 neighbors: [8,15,16,23,42]
}

To find data from neighbors, use the $in "operator":

要查找来自邻居的数据,请使用$in "operator"

db.nodes.find({nodeIndex:{$in: [8,15,16,23,42]}});

You can use field selectionto limit results to the relevant data.

您可以使用字段选择将结果限制为相关数据。

db.nodes.find({nodeIndex:{$in: [8,15,16,23,42]}}, {myData:1});

回答by jkschneider

Specialized Distributed Graph Databases

专业的分布式图数据库

I know this is sounds a little far afield from the OPs question about Mongo, but these days there are more specialized graph databases that excel at this kind of work and may be much easier for you to use, especially on large graphs.

我知道这听起来与关于 Mongo 的 OP 问题有点相距甚远,但是现在有更专业的图形数据库在此类工作中表现出色,并且可能更容易使用,尤其是在大型图形上。

There is a comparison of 7 such offerings here: https://docs.google.com/spreadsheet/ccc?key=0AlHPKx74VyC5dERyMHlLQ2lMY3dFQS1JRExYQUNhdVE#gid=0

这里有 7 个这样的产品的比较:https: //docs.google.com/spreadsheet/ccc?key=0AlHPKx74VyC5dERyMHlLQ2lMY3dFQS1JRExYQUNhdVE#gid=0

Of the three most significant open source offerings (Titan, OrientDB, and Neo4J), all of them support the Tinkerpop Blueprints interface. So for a graph that looks like this...

在三个最重要的开源产品(Titan、OrientDB 和 Neo4J)中,它们都支持 Tinkerpop Blueprints 界面。所以对于看起来像这样的图表......

enter image description here

在此处输入图片说明

... a query for "all the people that Juno greatly admires who she has known since the year 2011" would look like this:

...查询“自 2011 年以来朱诺非常钦佩的所有她认识的人”的查询将如下所示:

Iterable<Vertex> results = juno.query().labels("knows").has("since",2011).has("stars",5).vertices()

This, of course, is just the tip of the iceberg. Pretty powerful stuff!

当然,这只是冰山一角。好强大的东西!

If you have to stay with Mongo

如果你必须留在Mongo

Think of Tinkerpop Blueprints as the "JDBC of storing graph structures" in various databases. The Tinkerpop Blueprints API has a specific MongoDB implementation that would work for you I'm sure. Then using Tinkerpop Gremlin, you have all sorts of advanced traversal and search methods at your disposal.

将 Tinkerpop 蓝图视为各种数据库中的“存储图形结构的 JDBC”。Tinkerpop Blueprints API 有一个特定的 MongoDB 实现,我敢肯定它对你有用。然后使用 Tinkerpop Gremlin,您可以使用各种高级遍历和搜索方法。

回答by Michail Michailidis

MongoDBwill introduce native graph capabilities in version 3.4and it could be used to store graph stuctures and do analytics on them although performance might not be that good compared to native graph databases like Neo4jdepending on the cases but it is too early to judge.

MongoDB将在3.4 版本中引入原生图功能,它可用于存储图结构并对其进行分析,尽管与Neo4j等原生图数据库相比,性能可能不如Neo4j好,但现在判断还为时过早。

Check those links for more information:

检查这些链接以获取更多信息:

回答by Rohit Sood

MongoDB can simulate a graph using a flexible tree hierarchy. You may want to consider neo4j for strict graphing needs.

MongoDB 可以使用灵活的树状层次结构来模拟图。您可能需要考虑使用 neo4j 来满足严格的图形需求。