如何返回与我匹配的节点相关的节点的属性?

时间:2017-04-05 02:31:57

标签: neo4j

我正在尝试查询以下内容:

列出学生及其导师的姓名。

Image of the schema for my database

我使用以下查询:

MATCH (mName:Faculty)-[:Mentors]->(sName:Student) RETURN sName,mName

但是当我运行它时,我得到了导师的薪水和学生的分类。我想要每个人的名字。我试过这个:

MATCH (mName:Faculty)-[:Mentors]->(sName:Student)<-[:S2P]-(person) RETURN person.Name,mName

但是这有语法错误。

1 个答案:

答案 0 :(得分:0)

您正在将个人信息划分到自己的节点中,我认为这不是在图表数据库中对此进行建模的最佳方式。我建议将这些数据合并到:Student和:Faculty节点,尽管您可以在其中添加:Person标签,以便将其视为:其他查询中的人员。

使用您当前的模型,此查询应该有效:

MATCH (facultyPerson)<-[:F2P]-(:Faculty)-[:Mentors]->(:Student)-[:S2P]->(studentPerson) 
RETURN facultyPerson.Name as faculty, studentPerson.Name as student

如果您想在每位教职员工下收集学生姓名,您可以使用COLLECT():

MATCH (facultyPerson)<-[:F2P]-(:Faculty)-[:Mentors]->(:Student)-[:S2P]->(studentPerson) 
RETURN facultyPerson.Name as faculty, collect(studentPerson.Name) as students

如果您整合了节点数据,那么name,address和dob是每个的属性:Faculty和:Student,您的查询将变为:

MATCH (fac:Faculty)-[:Mentors]->(student:Student)
RETURN fac.Name as faculty, collect(student.Name) as students

另外,我建议使用camel case作为属性名称。

相关问题