Storing a graph in mongodb Storing a graph in mongodb database database

Storing a graph in mongodb


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.


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