本文介绍了如何在Neo4j数据库中的关系属性之一条件下编写Cypher查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Neo4j的新手,它试图创建一个查询,将节点和关系列出为关键字为"id = 0001" 的图形,如下所示:

I am new to Neo4j and trying to create a query listing nodes and relationships into a graph with keyword as "id=0001" as below:

(a)-[id:'0001',ref_id:null]->(b)-[id:'0002',ref_id:'0001']->(c)-[id:' 0003',ref_id:'0002']->(d)

起始节点为(a),因为它与id = 0001有关系

Start Node will be (a) since it has relationship with id=0001

但是数据库也存在我不想要的关系:

But the database also exists relationships which I don't want:

(a)-[id:'2001',ref_id:null]->(b)-[id:'2002',ref_id:'2001']->(c)
(a)-[id:'3001',ref_id:null]->(b)-[id:'3002',ref_id:'3001']->(c)

(a) - [id:'2001', ref_id:null] -> (b) - [id:'2002', ref_id:'2001'] -> (c)
(a) - [id:'3001', ref_id:null] -> (b) - [id:'3002', ref_id:'3001'] -> (c)

结果应仅包括:

(a)-[0001]-(b)-[0002,0001]-(c)-[0003,0002]-(d)

(a)-[0001]-(b)-[0002, 0001]-(c)-[0003,0002]-(d)

我知道如何在像Oracle和MySQL这样的SQL数据库中创建此查询,我可以在"where"条件下使用查询.例如:

I know how to create this query in SQL database like Oracle and MySQL, I can use query with "where" condition. For example:

Select * 
from table_a parent, (select * from table_a) child 
where child.ref_id = parent.id

然后,我可以在Java中循环结果集以查找所有关系.但这很愚蠢.

Then I can loop the result set in Java to find all relationships. But this is stupid.

我认为查询应类似于:

Match (n)-[r:RELTYPE]->(m) WHERE {some conditions at here} RETURN n,r,m

请帮助我,谢谢!

Yufan

推荐答案

您可以使用命名关系并在WHERE子句中进行过滤:

You could either use named relationships and filter in WHERE clause:

match p = (a)-[r1:TYPE]->(b)-[r2:TYPE2]->(c)
where r1.id='0001' and r2.id='0002' and r2.ref_id='0001'
return p

请注意,Neo4j不允许使用具有空值的属性.因此,第一个关系将没有ref_id.

Please note that properties having null value are not allowed in Neo4j. So the first relationship won't have a ref_id.

上述表示法是将条件放入match的快捷方式:

For the above notation is a shortcut by putting the conditions into the match:

match p = (a)-[r1:TYPE {id:'0001'}]->(b)-[r2:TYPE2 {id:'0002', ref_id:'0001'}]->(c)
return p

附带说明:我不确定您在关系属性中使用idref_id的方式是否是对数据建模的好方法.也许您可以使用更多详细的关系类型-但是,如果不了解域,那么实际上不可能在这里给出好的建议.

On a side note: I'm not sure if the way you're using id and ref_id in relationship properties is a good way to model your data. Maybe you can use more verbose relationship types - however without understanding the domain it's not really possible to give a good advice here.

这篇关于如何在Neo4j数据库中的关系属性之一条件下编写Cypher查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:51