获取arangodb中给定节点的已连接节点列表

时间:2016-04-27 13:49:53

标签: arangodb aql arangojs

我在Arangodb中构建了一个图表。

我很难达到以下要求。

给定一个节点,我需要一个连接到它的所有节点的列表以及它所连接的深度。

实施例

Customer2 - >在深度为1时连接到Customer1, Customer3 - >在深度为2时连接到Customer1,依此类推..

请帮助我实现这一目标。

2 个答案:

答案 0 :(得分:2)

假设你using the pattern matching traversals这很容易实现。

让我们使用Knows Graph exampleEve处开始遍历尝试此操作:

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" 
    } 
  } 
]

遍历中的每个步骤都将映射到列表中的一个对象。您还可以使用 e dges的_from_to属性(知道)来连接 v ertices(人员)

也只能看路径;你可以使用路径属性:

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

我们只返回迭代结束点的路径,将其限制为2,以便更好地进行概述;以下是结果路径之一:

[
  ...
  { 
    "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" 
        } 
      ] 
    } 
  } 
]

答案 1 :(得分:-1)

你需要这个

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