问题
I am trying to create a relationship in loop from both top list and bottom list .I am trying to connect the top loop with the bottom loop .I really appreciate any help.Thanks.
UNWIND [{id:"1",name:"b1",year:"2010"},
{id:"2",name:"d1",year:"2011"},
{id:"3",name:"e1",year:"2013"}] as user
MERGE (u:User {id: user.id, name: user.name,year:user.year})
UNWIND [{id:"21",name:"b",year:"2010"},
{id:"41",name:"d",year:"2011"},
{id:"51",name:"e",year:"2013"}] as w
MERGE (y:W {id: w.id, name: w.name,year:w.year})
MERGE (u)-[:SHARE]->(y)
Error:
WITH is required between MERGE and UNWIND (line 8, column 1 (offset: 192))
"unwind [{id:"21",name:"b",year:"2010"},"
^
Neo.ClientError.Statement.InvalidSyntax
回答1:
If you're trying to connect all of the User
nodes to all of the W
nodes then you could just switch the first MERGE
and the second UNWIND
:
UNWIND [{id:"1",name:"b1",year:"2010"},
{id:"2",name:"d1",year:"2011"},
{id:"3",name:"e1",year:"2013"}] as user
UNWIND [{id:"21",name:"b",year:"2010"},
{id:"41",name:"d",year:"2011"},
{id:"51",name:"e",year:"2013"}] as w
MERGE (u:User {id: user.id, name: user.name,year:user.year})
MERGE (y:W {id: w.id, name: w.name,year:w.year})
MERGE (u)-[:SHARE]->(y)
回答2:
Here's what you can do, but I have to agree with @Brian that you'd be better off programmatically generating the statements as per his comment.
WITH [{id:"1",name:"b1",year:"2010"},
{id:"2",name:"d1",year:"2011"},
{id:"3",name:"e1",year:"2013"}] as user,
[{id:"21",name:"b",year:"2010"},
{id:"41",name:"d",year:"2011"},
{id:"51",name:"e",year:"2013"}] as w
foreach (i in range(0,length(user)-1) |
MERGE (u:User {id: (user[i]).id, name: (user[i]).name,year:(user[i]).year})
MERGE (y:W {id: (w[i]).id, name: (w[i]).name,year:(w[i]).year})
MERGE (u)-[:SHARE]->(y))
来源:https://stackoverflow.com/questions/30217665/create-relationship-and-merge-node-in-loop-in-neo4j