简体   繁体   中英

Getting list of connected nodes for a given node in arangodb

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.

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 v ertices (persons) with the _from and _to attributes of the e dges (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" 
        } 
      ] 
    } 
  } 
]

you need this

FOR col in collection
    FOR node,edge,path IN 1..50 OUTBOUND col GRAPH "graph_name"
        LET sub = (
            FOR vertices in path.vertices
                RETURN vertices._id
            )
        RETURN DISTINCT [node._id,LENGTH(sub)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM