简体   繁体   中英

Filter neighbour's INBOUND vertices with path labels in ArangoDB

I have the following graph:

在此处输入图像描述

I'd like to write an AQL query that returns all vertices which are neighbor's INBOUND vertices colored in RED from the start vertex colored in GREEN.

I tried the following AQL to retrieve red vertices from the green vertex.

WITH collection_A, collection_W
LET A_Neighbors = (FOR t IN collection_edges
                    FILTER t._to == 'collection_W/W'
                    RETURN t._from)
                    
let all_w = []
for item in A_Neighbors
    let sub_w = (for v1 in collection_edges
                        FILTER v1._to == item
                        return v1 )
    return APPEND(all_w, sub_w)

Is there any good solution other than this? Because I'm not sure this gives the correct values for start vertex collection_W/W .

My collection_edges contains following two kind of documents.

{
 _from: collection_W/w,
 _to: collection_A/a,
 label: INBOUND
}

and

{
 _from: collection_A/a,
 _to: collection_W/w,
 label: OUTBOUND
}

Given the diagram, I would suggest using a graph traversal specific [min[..max]] value, like this (using an anonymous graph ):

WITH collection_A, collection_W
FOR vertex IN 2 ANY 'collection_W/W' // green start node
    collection_edges
    RETURN vertex

The [min[..max]] value can be a range ( 1..3 ) or it can be a single value ( 1 ).

  • 0 will return the start node
  • 1 will return adjacent nodes
  • 2 will skip the adjacent nodes and return only nodes at the next level (if any)
  • 2..999 will return all nodes (up to 999 hops away) from the start node

Further, if you want to make sure that you're only returning nodes from a specific collection, add a filter for that:

WITH collection_A, collection_W
FOR vertex IN 2 ANY 'collection_W/W' // green start node
    collection_edges
    FILTER IS_SAME_COLLECTION('collection_W', vertex)
    RETURN vertex

You can also filter on edges (if you've added a specific attribute/value to your edges):

WITH collection_A, collection_W
FOR vertex, edge IN 2 ANY 'collection_W/W' // green start node
    collection_edges
    FILTER edge.someProperty == 'someValue' // only return vertices that are beyond matching edges
    RETURN vertex

Or limit the traversal with PRUNE :

WITH collection_A, collection_W
FOR vertex, edge IN 2 ANY 'collection_W/W' // green start node
    collection_edges
    PRUNE edge.someProperty == 'someValue' // stop traversal when this is matched
    RETURN vertex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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