How to connect nodes in common


Im working in a project using cypher and im having issues to do some stuff. Firstly I must say that it's my first time with this and I feel a bit stucked.

Using neo4jBrowser y input this:

1- CREATE (Money_Monster:Film {name:"Money Monster"})
RETURN Money_Monster

2- MATCH (Money_Monster:Film {name:"Money Monster"})
FOREACH (actor in ['George Clooney', 'Julia Roberts', 'Caitriona Balfe', 'Giancarlo Esposito'] | CREATE (Money_Monster)-[:acting]->(:Film {name:actor}))

3- MATCH (Money_Monster {name:"Money Monster"})-[:acting]->(requiredactors)
RETURN Money_Monster, requiredactors

Then I do the same with Ocean's Eleven, and i get two differents graphs (uploaded in one photo because i have some restrictions as a new user)

So now I would like to do something like that:

Untitled Diagram

I mean, I would like to show the actors in common between two films...

How I could connect the nodes in common between two other nodes?

I wish I have explained properly :smiley:

In the image you provided, all the actor nodes are directed towards the film. But in your neo4j output, it is in reverse. And also that the actors also came under the label Film rather Actor.
The mistake is at 3rd line of your query 2. It should be as below.

CREATE (Money_Monster)<-[:acting]-(:Actor {name:actor})
With this, all the directions get reversed and all the actors come under a label Actor.

Now you have two nodes with same name George Clooney and Julia Roberts .
Delete any one node of same name nodes and create acting with another node.

1- CREATE (Money_Monster:Film {name:"Money Monster"})
RETURN Money_Monster

2- MATCH (Money_Monster:Film {name:"Money Monster"})
FOREACH (actor in ['George Clooney', 'Julia Roberts', 'Caitriona Balfe', 'Giancarlo Esposito'] | CREATE (Money_Monster)<-[:acting]-(:Actor {name:actor}))

3- CREATE (Ocean_Eleven :Film {name:"Ocean's Eleven"}) RETURN Ocean_Eleven


MATCH (Money_Monster:Film {name:"Ocean's Eleven"})
FOREACH (actor in ['George Clooney', 'Julia Roberts', 'Brad Pitt', 'Matt Demon'] | CREATE (Money_Monster)<-[:acting]-(:Actor{name:actor}))

Now delete George Cloone, Julia Roberts nodes of movie Money Monster.
just place the cursor on George Cloone, Julia Roberts nodes. you will get id's of those nodes.

match (n:Actor) where id(n) in [ids separated by comma] detach delete n

match (a:Actor{name:'George Clooney'}), (b:Actor{name:'Julia Roberts'}), (c:Film{name:"Money Monster"})
create (a)-[:acting]->(c),(b)-[:acting]->(c)

Hope this helpful to you

1 Like

Thank you for your time and your answer!

I have some doubts now, how should be the command to return the graph?, I mean, the RETURN command so I get the graph represented as I showed before.

Sorry for being such a noob :frowning:

sorry, i didn't get your doubt.
If you want to see your complete graph, try below
This will returns your created graph.

If you are asking how to create the complete graph as of the picture, try below.

MERGE (a:Film {name:"Money Monster"})
MERGE (b:Film {name:"Ocean's Eleven"})

MERGE (c:Actor {name:'George Clooney'})
MERGE (d:Actor {name:'Julia Roberts'})
MERGE (e:Actor {name:'Caitriona Balfe'})
MERGE (f:Actor {name:'Giancarlo Esposito'})
MERGE (g:Actor {name:'Brad Pitt'})
MERGE (h:Actor {name:'Matt Demon'})

MERGE (c)-[:acting]->(a)
MERGE (d)-[:acting]->(a)
MERGE (e)-[:acting]->(a)
MERGE (f)-[:acting]->(a)
MERGE (c)-[:acting]->(b)
MERGE (d)-[:acting]->(b)
MERGE (g)-[:acting]->(b)
MERGE (h)-[:acting]->(b)

Just copy and paste it neo4j. This will create the graph you want.
Hope it helps. If not please elaborate your doubt.

1 Like

Great! Thank you so much!!!

1 Like