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

时间:2022-04-08 18:04:03

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

2 个解决方案

#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:

如果您尝试将所有用户节点连接到所有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)

#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.

这是你可以做的,但我必须同意@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))

#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:

如果您尝试将所有用户节点连接到所有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)

#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.

这是你可以做的,但我必须同意@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))