简体   繁体   English

AQL Arango-使用Edge获取顶点和邻居

[英]AQL Arango - Get Vertex and Neighbors using Edges

Coming from neo4j and new to Arango. 来自neo4j和new到Arango。

I am trying to query my data and build a corresponding data structure. 我正在尝试查询我的数据并建立相应的数据结构。

So I have something like: 所以我有这样的事情:

    Circle A
    /       \
 Square A    Circle B 
            /     \         \
       Circle C   Square B  Square D

Circle are stored in a document collection. 圆存储在文档集合中。 Square are stored in a document collection. 方格存储在文档集合中。

I then have two edge Collections HAS_CIRCLE and HAS_SQUARE which correspond appropriately. 然后,我有两个对应的边缘集合HAS_CIRCLE和HAS_SQUARE。

I know I want Circle B and its neightbors - in a structure like so. 我知道我想要Circle B和它的倍数-这样的结构。

{
    circle: {data from Circle B},
    parents: [{data from Circle A}],
    children: [{data from Circle C}],
    squares: [{data from Square B}, {data from Square D}]
}

*Also to note I am not looking to nest this structure. *还要注意,我不想嵌套此结构。 Like when I want {data from Circle A} in parents - I don't expect this to also have parents, children, squares - just literally looking for the metadata contained in that node. 就像当我想要{Circle A中的数据}放在parents -我不希望它也有父级,子级和正方形-只是从字面上查找该节点中包含的元数据。

I know I can start like this... but I quickly get lost. 我知道我可以这样开始...但是我很快就迷路了。 And even when doing the basics - I can't seem to collect it up properly and associate the array to a key. 甚至在进行基础操作时-我似乎也无法正确收集它并将数组与键关联。

FOR c in Circle
    FILTER c.name === 'Circle B'
    FOR hc in HAS_CIRCLE
        FILTER hc._from === c._id

You must have missed the trip to the graph documentation in ArangoDB . 您一定错过了ArangoDB中的图形文档之旅 For sure you can use document queries with classical joins (like you tried) to do graph iterations and map it similar as you would do it in traditional SQL on any other RDBMS. 当然,您可以将文档查询与经典联接一起使用(就像您尝试过的那样)来进行图迭代,并像在其他任何RDBMS上的传统SQL中一样进行映射。

However, ArangoDB unveils its real graphing power, if you use the pattern matching traversals in queries like this: 但是,如果您在以下查询中使用模式匹配遍历 ,则ArangoDB会展现其真正的制图能力:

FOR vertex, edge, path IN 
  1..5 
  OUTBOUND
  'circle/A'
  GRAPH circesAndSquares 
    FILTER edge.name == 'Circle B'
     RETURN {vertices: vertex, edges: edge, paths: path}

edge will contain the edge document of the current traversal step, vertex the vertex. edge将包含当前遍历步骤的边缘文件, vertex的顶点。 FILTER ing them will hide the non matching documents from the RETURN statement. FILTER它们会在RETURN语句中隐藏不匹配的文档。 Paths can be filtered on iteration depths, which then in term may abort a traversal: 可以在迭代深度上过滤路径,这可能会导致遍历失败:

FILTER path.edges[1].name == 'Circle B'

You can also filter on any iteration depth: 您还可以过滤任何迭代深度:

FILTER path.vertices[*].isValid == true

The examples in the documentation demonstrate howto work with named and anonymous graphs and howto insert your data into ArangoDB. 文档中的示例演示了如何使用命名图和匿名图以及如何将数据插入ArangoDB。 ArangoDB has a special edge collection type which implicitely knows and enforces _from and _to attributes on edge documents - but aside of this restriction you can fill it with arbitrary documents. ArangoDB有一个特殊的边缘集合类型 ,它隐式知道并在边缘文档上强制使用_from_to属性-但是_to限制外,您还可以用任意文档填充它。

You can also combine regular AQL queries with graph traversals, as demonstrated in this example . 您还可以将常规AQL查询与图遍历相结合,如本示例所示

much thanks to @dothebart - this did get me pointed in the right directions. 非常感谢@dothebart-这确实使我指出了正确的方向。

my query ended up looking like. 我的查询最终看起来像。 still not 100% on if this is the most ideal but yields the results i was looking for. 仍然不是100%,如果这是最理想的,但是会产生我一直在寻找的结果。

FOR c IN Circle
  FILTER c.name == 'Circle B'
        RETURN {
            "circle" : c,
            "parents":  ( FOR parents IN INBOUND b._id HAS_CIRCLE RETURN parents )),
            "children": ( FOR children IN OUTBOUND b._id HAS_CIRCLE RETURN children ),
            "squares":  ( FOR squares IN OUTBOUND b._id HAS_SQUARE RETURN squares )
        }

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

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