0

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

For exa. as in below image, enter image description here

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" }
kerry
  • 757
  • 6
  • 16
jay.jivani
  • 1,560
  • 1
  • 16
  • 33

1 Answers1

0

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.
kerry
  • 757
  • 6
  • 16