Storing a graph in 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).


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});

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

在这里输入图像描述

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


请访问http://www.mongodb.org/display/DOCS/Trees+in+MongoDB寻求灵感。

链接地址: http://www.djcxy.com/p/52370.html

上一篇: 使用有向和无向边缘

下一篇: 在mongodb中存储图形