Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-05-2020 09:28 AM
Hello!
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:
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
Solved! Go to Solution.
03-05-2020 11:04 PM
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
4-
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
03-05-2020 11:04 PM
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
4-
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
03-06-2020 11:47 AM
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
03-06-2020 11:06 PM
sorry, i didn't get your doubt.
If you want to see your complete graph, try below
MATCH (n) RETURN n
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.
03-08-2020 11:10 AM
Great! Thank you so much!!!
All the sessions of the conference are now available online