如何在neo4j中显示所有节点和两个节点之间的关系?

时间:2021-01-02 05:29:15

标签: neo4j cypher

创建 Year 和 Month 节点并将它们连接到 Employee 节点。 这是密码:

MERGE (y:Year {year:toInteger(line.YearofJoining)})
MERGE (m:Month {month:line.MonthNamofJoining)})
MERGE (y)-[:MONTH]->(m)

MERGE (a:Employee {empid:line.EmpID, firstname:line.FirstName, lastname:line.LastName, 
gender:line.Gender})
MERGE (m)-[:EMPLOYEE]->(a)

如何显示节点和两个节点之间的关系?例如,如果我选择两个不同的员工 id,这里我想显示两个员工之间的关系是什么(将两个员工作为公共属性名字、姓氏、月份、年份等。)

提前致谢

1 个答案:

答案 0 :(得分:0)

通过这个查询你可以得到两个节点之间的所有关系

MATCH (n1)-[r]->(n2) 
RETURN {type: type(r), nodes: {n1: n1{.*}, n2: n2{.*}}}

OR

MATCH (n1)-[r]->(n2) 
RETURN {type: type(r), nodes: {n1: collect(distinct n1{.*}), n2: collect(distinct n2{.*})}}

通过公共属性获取员工:

MATCH (e:Employee)-[]-(m:Month) 
return {
  month: m.month, // You can replace it with any property you want to group for example "gender: e.gender"
  employees: collect(distinct e{.*})
} as byMonth

编辑

<块引用>

如何了解与给定 Employee id 相似的其他员工。例如,我在一家公司工作(我的年龄、地点、加入年份作为数据),我想知道公司中具有相同年龄、加入年份、加入月份、地点等类似关系的员工. 写给我

下面的查询应该可以工作

match (employee:Employee) where employee.empid= 1
optional match (employee)-[:EMPLOYEE]-(month:Month{month: 10})-[:EMPLOYEE]-(other:Employee) 
optional match (other) where other.monthOfJoining = employee.monthOfJoining or other.yearOfJoining = employee.yearOfJoining or other.age = employee.age
return employee, other

编辑 2

获取与两个或多个节点相关的所有公共节点的计数

MATCH (node1:Employee)-->(r)<--(node2:Employee)

with count(r) as maxCountRelation, node1{.*} as e1, node2{.*} as e2

return {commonRelation: maxCountRelation, employees: collect(distinct e1)+collect(distinct e2)} as result order by result.commonRelation desc limit 1