我假设继承是指从祖父母到父母再到孩子再到孙子等的能力... Arango 支持遍历并且能够非常快速地遍历这些类型的关系。例如,要复制上面从节点 D 开始并获取节点 B 和 A 的示例,您可以执行以下操作:
// Find all nodes that are named d
let dNodes = (FOR test in test2
FILTER test.name == 'd'
RETURN test)
//Traverse outbound relationships starting at the dNodes and return up to 2 nodes up the hierarchy
FOR node in dNodes
FOR v,e IN 1..2 OUTBOUND node
testEdge
RETURN v
在性能方面,我已经遍历了具有数千个节点的不规则层次结构,没有性能问题,也不需要缓存任何东西。但是请记住,这里没有魔法,无论数据库引擎如何,糟糕的数据模型都会造成麻烦。
这里有一些性能信息,如果你想回顾和玩它here
遍历多个边(关系类型)与我们之前的示例非常相似。要使用层次结构(橙色)边和关系(绿色)边找到从 E 到 F 的路径,我们可以这样做:
// Find all nodes that are named E
let eNodes = (FOR test in test3
FILTER test.name == 'E'
RETURN test
)
// Start in node E and go upto three steps
// Traverse the hierarchy edges in any direction (so that we can find parents and child nodes)
// Traverse the relatedto (green) edges in the outbound direction only
// Filter the traversal to items that end in vertice F and return the path (E<-B->C->F)
FOR node in eNodes
FOR v,e,p IN 1..3 ANY node
parentOf, OUTBOUND relatedTo
FILTER v.name == 'F'
RETURN p
或者如果我们只想要 E 和 F 之间的最短路径,我们可以这样做:
let eNodes = (FOR test in test3
FILTER test.name == 'E'
RETURN test
)
//Find shortest path between node E and F and return the path (E<-B->C->F)
FOR node in eNodes
FOR v, e IN ANY SHORTEST_PATH
node TO 'test3/F'
parentOf, OUTBOUND relatedTo
RETURN e
请注意,我只是在上面的代码中使用了“F”记录的 id,但我们可以像搜索“E”记录一样使用名称搜索记录。
还请注意,我们在数据库中将示例的边数据创建为有向边:parentOf 边是从父级到子级创建的(例如:A 到 B),对于绿色关系边,我们按字母顺序创建它们(例如:B 到 C )。