简体   繁体   中英

How can i display the all nodes and relationships between two nodes in neo4j?

Created Year and Month nodes and connected them to Employee node. Here is the Cypher:

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)

How can I show the nodes and relationship between two nodes? for example, if I select two different employee id, here I want to display what are the relationship between two employees (will two employees as common properties first name, Lastname, month, year etc..)

Thanks in advance

With this query you can get all the relationship between two nodes

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{.*})}}

Get employees by common properties:

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

EDIT

How to know other employees similar to the given Employee id. for example, I was working for a company (my age, location, year of joining as data) and I want to know the employees in the company who has similar relationships like same age, year of joining, the month of joining, location etc. w.r.t to me

Below query should work

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

EDIT 2

Get count of all common nodes related to two or more nodes

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

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