简体   繁体   中英

ArrangoDB - Get All Possible Path From Given Value

I want a query to get all possible paths that start from a given vertex.

For exa. as in below image, 在此处输入图像描述

I want to find all paths that start from "Covid/12109" with Query.

So it returns like this

    { "_from":"Covid/12109","_to":"Covid/12110" }
    { "_from":"Covid/12110","_to":"Covid/12111" }
    { "_from":"Covid/12110","_to":"Covid/12115" }
    { "_from":"Covid/12110","_to":"Covid/12114" }
    { "_from":"Covid/12111","_to":"Covid/12115" }
    { "_from":"Covid/12111","_to":"Covid/12114" }
    { "_from":"Covid/12112","_to":"Covid/12110" }
    { "_from":"Covid/12112","_to":"Covid/12113" }
    { "_from":"Covid/12112","_to":"Covid/12114" }

And if i want to start from "Covid/12110" then it should return like this

    { "_from":"Covid/12110","_to":"Covid/12111" }
    { "_from":"Covid/12110","_to":"Covid/12115" }
    { "_from":"Covid/12110","_to":"Covid/12114" }
    { "_from":"Covid/12111","_to":"Covid/12115" }
    { "_from":"Covid/12111","_to":"Covid/12114" }
    { "_from":"Covid/12112","_to":"Covid/12110" }
    { "_from":"Covid/12112","_to":"Covid/12113" }
    { "_from":"Covid/12112","_to":"Covid/12114" }

And if i want to start from "Covid/12112" then it should return like this

    { "_from":"Covid/12112","_to":"Covid/12110" }
    { "_from":"Covid/12112","_to":"Covid/12113" }
    { "_from":"Covid/12112","_to":"Covid/12114" }
    { "_from":"Covid/12110","_to":"Covid/12111" }
    { "_from":"Covid/12110","_to":"Covid/12115" }
    { "_from":"Covid/12110","_to":"Covid/12114" }
    { "_from":"Covid/12111","_to":"Covid/12115" }
    { "_from":"Covid/12111","_to":"Covid/12114" }

Graph traversal is your friend here. There are several ways to accomplish this, but you might start by:

FOR c IN Covid
    FILTER c._key == '12109'
    FOR v,e IN 1..9 OUTBOUND c
        `has`
        OPTIONS { uniqueVertices: true }
        RETURN e

The name of your edge collection ('has') is tricky because HAS is an AQL keyword (see the docs about naming things with keywords). I've enclosed this in backticks (the AQL escape char), but you could also create a named graph , which (I believe) is much more flexible.

Looking at the query:

  • We first find documents in the "Covid" collection that match a key. This is optional, you could also swap the "c" in the graph traversal with a document id like "Covid/12109"
  • FOR v,e represents "vertices" v and "edges" e to return
  • 1..9 is the number of traversal "jumps" to perform. This can be any number ( 2 ) or range ( 5..27 )
  • OUTBOUND refers to the path direction to traverse. Other options here are OUTBOUND and ANY
  • { uniqueVertices: true } tells the engine to keep track of which vertices it's returned and not duplicate them on output. See the docs here
  • RETURN e will return edge ("has") documents. RETURN v would return vertex ("Covid") documents.

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