How to create 2 hop path in my case?

Hi, everybody,

I have community nodes (c: Community) and user nodes (u:user)

I want to create for each community node one relationship with usernode that is associated with another usernode which is connected with the community node that alredy in database

i want to get something like

(c4: Community) <- [: MEMBER_OF] - (u5: user) - [] - (u6: user) - [: MEMBER_OF] -> (c5: Community)

where (c4: Community) is community that alredy in database and <- [: MEMBER_OF] - (u5: user) - [] - (u6: user) - [: MEMBER_OF] -> (c5: Community) new nodes and relationships

Thanks for your attention and answers

Hello @yevheniy.derykot :slight_smile:

So you want to create nodes u5, u6 and c5 and all relationships? Do you have properties to create them?

Regards,
Cobra

If I understand correctly, this is what you are looking for.

//Assuming c4 already exist in the db......
 MATCH (c4:Community {id: 1})
 WITH c4
 
//New entries.......
 MERGE (c5:Community {id: 2})
 MERGE (u5:User {name:"u5"})
 MERGE (u6:User {name:"u6"})
 
//Connect the users to their respective community........
 MERGE (u5)-[:MEMBER_OF]->(c4)
 MERGE (u6)-[:MEMBER_OF]->(c5)
 
//Guessing the relationship type between the users.....
 MERGE (u5)-[:FRIEND_OF]->(u6)
 RETURN c4, c5, u5, u6

Yes ids of communities. I think that first I need to collect the ids of community nodes.

Yes, this is very close to what I want to do

i tried this code and it didn't work

 MATCH (c:Community)
 WITH collect(DISTINCT c.id) AS ids
 return ids

 FOREACH (id IN ids |
 MERGE (c5:Community)
 MERGE (u5:user {name:"u5"})
 MERGE (u6:user {name:"u6"})
 MERGE (u5)-[:MEMBER_OF]->(c)
 MERGE (u6)-[:MEMBER_OF]->(c5)
 MERGE (u5)-[:FRIEND_OF]->(u6)
)

You also have the possiblity to create a full path in Cypher :slight_smile:

1 Like

Move this, 'return ids' to the bottom (after FOREACH loop).

1 Like

Thanks for the advice :smiley:

this is what happened

but I wanted each blue node (community node) to have a separate connection as in the picture below

image

You need to add the id value to the Community node in the FOREACH loop
FOREACH (id IN ids |
 MERGE (c5:Community {id: id})
 MERGE (u5:user {name:"u5"})
 MERGE (u6:user {name:"u6"})
 MERGE (u5)-[:MEMBER_OF]->(c)
 MERGE (u6)-[:MEMBER_OF]->(c5)
 MERGE (u5)-[:FRIEND_OF]->(u6)
)

got the same results.

I think the problem is in this line

MERGE (u5)-[:MEMBER_OF]->(c)

Yes. Correct the WITH statement:
MATCH (c:Community)
 WITH collect(DISTINCT c.id) AS ids, c
1 Like

Thanks for the help

That's what i get and this is not what I need

Now there are 7 community nodes in the database, so I need to create 7 new community nodes and 14 new user nodes

In your FOREACH loop you have only two user nodes:
MERGE (u5:user {name:"u5"})
MERGE (u6:user {name:"u6"})

yes, I see :upside_down_face: but how to automatically create the required number of links and nodes ?

It depends on the source of User node properties. Just like the Community ids, there should be a mechanism to loop through the User node properties to create new user nodes.