在 mongodb 中存储图表

发布于 2024-10-19 11:09:30 字数 351 浏览 1 评论 0原文

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

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

table 1 
NodeA, NodeB
NodeA, NodeC

table 2 
NodeA, item1
NodeA, item2
NodeB, item3 

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

︶ ̄淡然 2024-10-26 11:09:30

专业的分布式图形数据库

我知道这听起来与关于 Mongo 的 OP 问题有点遥远,但现在有更专业的图形数据库擅长这种工作,并且可能更容易使用,特别是在大型图形上。

这里对 7 种此类产品进行了比较:https://docs.google .com/spreadsheet/ccc?key=0AlHPKx74VyC5dERyMHlLQ2lMY3dFQS1JRExYQUNhdVE#gid=0

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

在此处输入图像描述

... 查询“所有朱诺非常钦佩她自 2011 年以来认识的人”看起来像这样:

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

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

如果您必须继续使用 Mongo,

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

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.

There is a comparison of 7 such offerings here: 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...

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:

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

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.

乙白 2024-10-26 11:09:30

我正在选择 mongo,也研究这种模式(无向图,从邻居查询信息)我认为到目前为止我喜欢的方式看起来像这样:

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

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

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

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

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

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

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:

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

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

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

db.nodes.find({nodeIndex:{$in: [8,15,16,23,42]}}, {myData:1});
喜爱皱眉﹌ 2024-10-26 11:09:30

MongoDB 将在版本 3.4 中引入本机图形功能,它可用于存储图形结构并对其进行分析,尽管与本机图形数据库(如Neo4j 取决于具体情况,但现在判断还为时过早。

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

  • $graphLookup(聚合)
  • < a href="https://www.mongodb.com/press/3_4" rel="nofollow noreferrer">MongoDB 3.4 加速现代企业的数字化转型

MongoDB will introduce native graph capabilities in version 3.4 and 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 Neo4j depending on the cases but it is too early to judge.

Check those links for more information:

小伙你站住 2024-10-26 11:09:30

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文