我在 Arangodb 中构建了一个图表。
我正在努力满足以下要求。
给定一个节点,我需要一个连接到它的所有节点的列表以及它连接到的深度。
例子:
Customer2 -> 在深度 1 处连接到 Customer1,Customer3 -> 在深度 2 处连接到 Customer1,依此类推..
请帮助我实现这一目标。
假设您正在使用模式匹配遍历,这很容易实现。
让我们使用Knows Graph 示例尝试此操作,开始遍历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"
}
}
]
遍历中的每一步都将映射到列表中的一个对象。您还可以将顶点(人)与边缘(知道)的和属性_from
连接_to
起来
也可以只看路径;您可以使用路径属性:
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"
}
]
}
}
]