Creating relationships based on array values in Neo4j

二次信任 提交于 2021-02-07 23:45:37

问题


I have two nodes representing two people:

(:Person {name:"John Smith"})
(:Person {name:"Jane Doe"})

Then I have a third node representing an article coauthored by these two people:

(:Article {title:"Some_article"}, {Coauthor:["John Smith", "Jane Doe"]})

My question is: Can I create a relationship between these nodes based on matching the names? Something like this:

MATCH (n1:Person {name:"Jane Doe"})
MATCH (n2:Article{Coauthor:"Jane Doe"})
CREATE (n2)-[:AUTHORED_BY]->(n1)

Is this possible or do I need to break up the array into separate node properties e.g. Coauthor_1, Coauthor_2 etc?

Thanks

Neo4j CE 3.0.1 on Windows 10


回答1:


You can use a loop for creating authorship relationships :

MATCH (a:Article {title:"some title"})
UNWIND a.Coauthor as author
MERGE (p:Person {name: author})
MERGE (a)-[:AUTHORED_BY]->(p)


来源:https://stackoverflow.com/questions/37861841/creating-relationships-based-on-array-values-in-neo4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!