cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Faster way to add many edges to particular node?

Rogie
Node Link

I have a query being built up through python that looks something like this:

for url in urls:
    q = '''Match (u:User) Where u.id=18284 
           Merge (:Url {{ url:"{url}" }})<-[:CONTAINS_URL]-(u)'''
    tx.run(q)

It seems wasteful to have to Match on the user over and over again. Is there a way to match it once and add all the corresponding url edges in some sort of batch?

1 REPLY 1

The most easy way would to pass in an array parameter and use UNWIND:

tx.run("MATCH (u:User{id:$id}) 
   WITH u 
   UNWIND $urls as url 
   MERGE (:Url{url:url})<-[:CONTAINS]-(u)", 
   id=18284, urls=urls)

Note you're passing named parameters to tx.run. Those will be resolved using the $ notation in your cypher string.