简体   繁体   English

CYPHER - 按原始顺序返回链接节点

[英]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!我真的很享受 Graph 和 Cypher 查询语言(来自 RDBMS 背景)的潜在力量,但我真的很难理解所涉及的一些概念!

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文件夹 4
  • Folder 5文件夹 5
  • Folder 3文件夹 3
  • Folder 2文件夹 2
  • Folder 1文件夹 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?我怎么可能 go 关于订购这些数据,还是我的查询需要完全重写?

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 .我认为您的问题来自多个路径p的事实,这就是您使用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:不要忘记,如果您要求数据库查找模式(n:Folder)-[:CHILD*]->(f:Folder { id: '1d05a36b-a67f-3fe7-a13a-6f12b1a38d26' }) ,数据库将给出你所有的可能性,所以在你的例子中:

  • 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 .如果你对这个结果集做一个distinct的,是的,你有结果F4, F5, F3, F2, F1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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