0

I have constructed a graph in Arangodb.

I'm struggling to get the below requirement.

Given a node, and i need a list of all the nodes connected to it along with the depth it is connected to.

Example:

Customer2 -> connected to Customer1 at depth of 1, Customer3 -> Connected to Customer1 at depth of 2 and so on..

Please help me in achieving this.

mahi
  • 59
  • 3

1 Answers1

2

Assuming you're using the pattern matching traversals this is easy achieveable.

Lets try this using the Knows Graph example starting the traversal at Eve:

db._query(`FOR v, e IN 1..3 OUTBOUND 'persons/eve' 
           GRAPH 'knows_graph' 
           RETURN {v: v, e: e}`)
[ 
  { 
    "e" : { 
      "_from" : "persons/eve", 
      "_id" : "knows/156", 
      "_key" : "156", 
      "_rev" : "156", 
      "_to" : "persons/alice" 
    }, 
    "v" : { 
      "_id" : "persons/alice", 
      "_key" : "alice", 
      "_rev" : "130", 
      "name" : "Alice" 
    } 
  }, 
  { 
    "e" : { 
      "_from" : "persons/alice", 
      "_id" : "knows/146", 
      "_key" : "146", 
      "_rev" : "146", 
      "_to" : "persons/bob" 
    }, 
    "v" : { 
      "_id" : "persons/bob", 
      "_key" : "bob", 
      "_rev" : "134", 
      "name" : "Bob" 
    } 
  }, 
  { 
    "e" : { 
      "_from" : "persons/bob", 
      "_id" : "knows/150", 
      "_key" : "150", 
      "_rev" : "150", 
      "_to" : "persons/charlie" 
    }, 
    "v" : { 
      "_id" : "persons/charlie", 
      "_key" : "charlie", 
      "_rev" : "137", 
      "name" : "Charlie" 
    } 
  }, 
  { 
    "e" : { 
      "_from" : "persons/bob", 
      "_id" : "knows/153", 
      "_key" : "153", 
      "_rev" : "153", 
      "_to" : "persons/dave" 
    }, 
    "v" : { 
      "_id" : "persons/dave", 
      "_key" : "dave", 
      "_rev" : "140", 
      "name" : "Dave" 
    } 
  }, 
  { 
    "e" : { 
      "_from" : "persons/eve", 
      "_id" : "knows/159", 
      "_key" : "159", 
      "_rev" : "159", 
      "_to" : "persons/bob" 
    }, 
    "v" : { 
      "_id" : "persons/bob", 
      "_key" : "bob", 
      "_rev" : "134", 
      "name" : "Bob" 
    } 
  }, 
  { 
    "e" : { 
      "_from" : "persons/bob", 
      "_id" : "knows/150", 
      "_key" : "150", 
      "_rev" : "150", 
      "_to" : "persons/charlie" 
    }, 
    "v" : { 
      "_id" : "persons/charlie", 
      "_key" : "charlie", 
      "_rev" : "137", 
      "name" : "Charlie" 
    } 
  }, 
  { 
    "e" : { 
      "_from" : "persons/bob", 
      "_id" : "knows/153", 
      "_key" : "153", 
      "_rev" : "153", 
      "_to" : "persons/dave" 
    }, 
    "v" : { 
      "_id" : "persons/dave", 
      "_key" : "dave", 
      "_rev" : "140", 
      "name" : "Dave" 
    } 
  } 
]

Each step in the traversal will map to one Object in the list. You can also connect the vertices (persons) with the _from and _to attributes of the edges (knows)

Only looking at the paths is also possible; you can use the path attribute:

db._query(`FOR v, e, p IN 2..2 OUTBOUND 'persons/eve' 
           GRAPH 'knows_graph' 
           RETURN {p: p}`)

We only return the paths for the end point of the iteration, limiting it to 2 to be a little better overviewable; Here is one of the resulting paths:

[
  ...
  { 
    "p" : { 
      "edges" : [ 
        { 
          "_from" : "persons/eve", 
          "_id" : "knows/159", 
          "_key" : "159", 
          "_rev" : "159", 
          "_to" : "persons/bob" 
        }, 
        { 
          "_from" : "persons/bob", 
          "_id" : "knows/153", 
          "_key" : "153", 
          "_rev" : "153", 
          "_to" : "persons/dave" 
        } 
      ], 
      "vertices" : [ 
        { 
          "_id" : "persons/eve", 
          "_key" : "eve", 
          "_rev" : "143", 
          "name" : "Eve" 
        }, 
        { 
          "_id" : "persons/bob", 
          "_key" : "bob", 
          "_rev" : "134", 
          "name" : "Bob" 
        }, 
        { 
          "_id" : "persons/dave", 
          "_key" : "dave", 
          "_rev" : "140", 
          "name" : "Dave" 
        } 
      ] 
    } 
  } 
]
dothebart
  • 5,972
  • 16
  • 40
  • Hi Dothebart. Thanks for your response. but that is not i'm looking at :( Actually i saw the one you posted in the arangodb documentation in their website but i need all the vertices connected to the given vertex along with the depth it is connected to. Say for the above knows example, the output should be as below: eve -> bob (depth 1) eve -> alice (depth 1) eve -> dave (depth 2 eve->bob->dave) eve -> charlie(depth 2 eve->bob->charlie) eve -> bob (depth 2 - eve->alice->bob) Any ideas?? Please help – mahi Apr 28 '16 at 11:24
  • in the path you actualy have the depth; you simply need to count the depth you access; `result[n].vertices[2]` => depth 2 – dothebart Apr 28 '16 at 13:01