How to create relationship within the same node based on property

Following scenario:

Node Person
person | boss

Peter | Bob
Bob | Karl
Peter | Karl
Karl |
...

Node Team
team| boss

ABC | Bob
CDE | Bob
XZY | Karl
...

What I'd like to create Relationships between all(!) persons and teams.

  1. Create relationships between person (=boss) and team boss. That is simple and works
    match(p:Person),(t:Team) where p.person= t.boss create (p)-[:IS_BOSS]->(t)

Result:
Node Person Node Team
person | boss

Peter | Bob
Bob | Karl -[IS_BOSS]->ABC,CDE
Peter | Karl
Karl | -[IS_BOSS]->XZY
...

  1. I would now like to assign all other persons to their respective team/boss as well. That is simple and works as well:
    match(p:Person),(t:Team) where p.boss= t.boss create (p)-[:MEMBER]->(t)

Result:
Node Person -[REL]-> Node Team
person | boss | team

Peter | Bob -[IS_MEMBER]->ABC,CDE
Bob | Karl -[IS_BOSS]->ABC,CDE
Peter | Karl -[IS_MEMBER]->ABC,CDE
Karl | -[IS_BOSS]->XZY

  1. I would also like to create [IS_BOSS] inside the People node to delete the Boss property. So that in the end, only persons are connected.

Any ideas?

It's a little tough to read those tables of results, as I can't tell what Cypher is being used to produce them.

One note first on this:
(:Person)-[:IS_BOSS]->(:Team)
You might consider :BOSS_OF instead, or otherwise reversing the direction, because currently :IS_BOSS points to :Team, which doesn't sound right, the team isn't the boss.
Likewise maybe :MEMBER_OF would work better since it points at :Team, since right now :MEMBER points to :Team, which may be confusing as team isn't a member.

As for connecting people, this should work (using the suggested :BOSS_OF and :MEMBER_OF for clarity):

MATCH (p)-[:MEMBER_OF]->()<-[:BOSS_OF]-(boss:Person)
MERGE (p)<-[:BOSS_OF]-(boss)

At that point you should be able to remove the boss property from all nodes.

1 Like