简体   繁体   English

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

[英]Getting list of connected nodes for a given node in arangodb

I have constructed a graph in Arangodb.我在 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.. Customer2 -> 以 1 的深度连接到 Customer1,Customer3 -> 以 2 的深度连接到 Customer1,依此类推。

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 : 让我们使用从Eve开始遍历的Knows Graph示例进行尝试:

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) 您还可以连接在V ertices(人)与_from_to 电子当选总监的属性(知道)

Only looking at the paths is also possible; 也只能看路径。 you can use the path attribute: 您可以使用path属性:

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; 我们只返回迭代终点的路径,将其限制为2,以便于更好地概述。 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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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