is there somesthing like "while" cycle?

22 views
Skip to first unread message

Александр Пересмешник

unread,
Apr 24, 2017, 4:34:44 PM4/24/17
to ArangoDB
I have a collection "items" with data like this:

{"_key": "item1"}
{"_key": "item2", "parent": "item1"}
{"_key": "item3", "parent": "item2"}
{"_key": "item4", "parent": "item3"}
{"_key": "item5", "parent": "item4"}
{"_key": "item6", "parent": "item5"}
 
I want make query that return list like this: ["item5", "item4", "item3", "item2", "item1"]

Query that can make this is:

LET start_item = "item5"
FOR item1 in items
FILTER item1.parent == start_item
  FOR item2 in items
  FILTER item3.parent == item2.parent
    FOR item3 in items
    FILTER item4.parent == item3.parent
      FOR item4 in items
      FILTER item5.parent == item4.parent
        FOR item5 in items
        FILTER item6.parent == item5.parent
          RETURN [item6.parent, item5.parent, item4.parent, item3.parent, item2.parent, item1.parent]

Is there way to make that query smaller? Something like "while" cycle with some condition like check "parent" exists?

This can be done using the Graph. But can make that without graph?
 

mpv1989

unread,
Apr 25, 2017, 4:02:34 AM4/25/17
to ArangoDB
I see no way to make your query smaller/better without using a graph. Your case fits perfect to a graph model. The query you want to implement is a typical graph traversal.
Reply all
Reply to author
Forward
0 new messages