在 Neo4j 中循环创建关系和合并节点

create relationship and merge node in loop in neo4j

我正在尝试从顶部列表和底部列表创建循环关系。我正在尝试将顶部循环与底部循环连接起来。我非常感谢任何 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)

错误:

WITH is required between MERGE and UNWIND (line 8, column 1 (offset: 192))
"unwind [{id:"21",name:"b",year:"2010"},"
 ^
 Neo.ClientError.Statement.InvalidSyntax

如果您尝试将所有 User 节点连接到所有 W 节点,那么您可以切换第一个 MERGE 和第二个 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)

这是您可以执行的操作,但我必须同意@Brian 的观点,您最好根据他的评论以编程方式生成语句。

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))