如何在neo4j中创建关系并在循环中合并/创建新节点

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

I am trying to create friend relationship to all the ids in the list but I am getting an error:

我正在尝试创建列表中所有ID的朋友关系,但我收到一个错误:

y already declared (line 2, column 41 (offset: 64))

"Merge (y:User{id:"21",name:"b"})
 Merge (y:User{id:"41",name:"d"})
 Merge (y:User{id:"51",name:"e"}) "
                                         ^
 Neo.ClientError.Statement.InvalidSyntax

I am trying to create a new user with id,name,year and also relationship with existing node at the same time .How can I do it ?Is there any other way also?I really appreciate any help.

我正在尝试创建一个具有id,name,year以及同时与现有节点的关系的新用户。我该怎么做?还有其他任何方式吗?我真的很感激任何帮助。

MATCH (u:User {id:"2"})
Merge (y:User{id:"21",name:"b",year:"2010"})
Merge (y:User{id:"41",name:"d",year:"2011"})
Merge (y:User{id:"51",name:"e",,year:"2013"}) 
MERGE (u)-[:FRIEND]->(y)

1 个解决方案

#1


Reusing identifier y won't work.

重用标识符y将不起作用。

Try

MATCH (u:User {id:"2"})
unwind [{id:"21",name:"b",year:"2010"},
        {id:"41",name:"d",year:"2011"},
        {id:"51",name:"e",year:"2013"}] as user
merge (y:User {id: user.id, name: user.name,year:user.year})
MERGE (u)-[:FRIEND]->(y)

#1


Reusing identifier y won't work.

重用标识符y将不起作用。

Try

MATCH (u:User {id:"2"})
unwind [{id:"21",name:"b",year:"2010"},
        {id:"41",name:"d",year:"2011"},
        {id:"51",name:"e",year:"2013"}] as user
merge (y:User {id: user.id, name: user.name,year:user.year})
MERGE (u)-[:FRIEND]->(y)