简体   繁体   中英

CYPHER - Return linked nodes in original order

I'm really enjoying the potential power of Graph & Cypher query language (coming from an RDBMS background), but I'm really struggling to get my head around some of the concepts involved!

What I'm attempting to do feels like it should be relatively simple... I have a very simple series of five nodes with a parent -> child relationship. In my mind, if I was to query for this, each node should be returned in the appropriate order. Here is the query as it stands -

MATCH p = (:Folder)-[:CHILD*]->(f:Folder { id: '1d05a36b-a67f-3fe7-a13a-6f12b1a38d26' })

WITH NODES(p) AS folders

UNWIND folders as folder

WITH folder RETURN DISTINCT folder

Here is what the result of my query looks as a graph -

在此处输入图像描述

However, once queried with the above query and getting the physical response, it's being represented in the following order -

  • Folder 4
  • Folder 5
  • Folder 3
  • Folder 2
  • Folder 1
╒══════════════════════════════════════════════════════════════════════╕
│"folder"                                                              │
╞══════════════════════════════════════════════════════════════════════╡
│{"name":"Folder 4","created_at":"2019-10-07 12:14:07","id":"5b6b3316-e│
│e57-3ca5-8f62-bed81b09ab7b","updated_at":"2019-10-07 12:14:07","parent│
│_folder_id":"2dabecfc-2018-3876-bc01-28922ebbb09d"}                   │
├──────────────────────────────────────────────────────────────────────┤
│{"name":"Folder 5","created_at":"2019-10-07 12:14:07","id":"1d05a36b-a│
│67f-3fe7-a13a-6f12b1a38d26","updated_at":"2019-10-07 12:14:07","parent│
│_folder_id":"5b6b3316-ee57-3ca5-8f62-bed81b09ab7b"}                   │
├──────────────────────────────────────────────────────────────────────┤
│{"name":"Folder 3","created_at":"2019-10-07 12:14:07","id":"2dabecfc-2│
│018-3876-bc01-28922ebbb09d","updated_at":"2019-10-07 12:14:07","parent│
│_folder_id":"a1344b93-ab69-398d-81c5-cb901ff8f0b0"}                   │
├──────────────────────────────────────────────────────────────────────┤
│{"name":"Folder 2","created_at":"2019-10-07 12:14:07","id":"a1344b93-a│
│b69-398d-81c5-cb901ff8f0b0","updated_at":"2019-10-07 12:14:07","parent│
│_folder_id":"a22eca18-57fd-364d-b965-d850724131e8"}                   │
├──────────────────────────────────────────────────────────────────────┤
│{"name":"Folder 1","created_at":"2019-10-07 12:14:07","id":"a22eca18-5│
│7fd-364d-b965-d850724131e8","updated_at":"2019-10-07 12:14:07"}       │
└──────────────────────────────────────────────────────────────────────┘

How can I possibly go about ordering this data or does my query need a complete rewrite?

Thanks!

Can you try this query:

MATCH p = (n:Folder)-[:CHILD*]->(f:Folder { id: '1d05a36b-a67f-3fe7-a13a-6f12b1a38d26' })
WHERE NOT ()-[:CHILD]->(n)
WITH NODES(p) AS folders
RETURN folders

I think that your problem comes from the fact that there are multiple paths p , and that's why you used the distinct .

Dont forget that if you ask the database to find the pattern (n:Folder)-[:CHILD*]->(f:Folder { id: '1d05a36b-a67f-3fe7-a13a-6f12b1a38d26' }) , the database will give you all the possibilities, so in your example:

  • F4 -> F5
  • F3 -> F4 -> F5
  • F2 -> F3 -> F4 -> F5
  • F1 -> F2 -> F3 -> F4 -> F5

And if you do a distinct on this result set, yes you have the result F4, F5, F3, F2, F1 .

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