Using unwind and python dict to populate database


I hope you all are doing well. I'm using for the moment py2neo however I'm not dead set on it. I'm trying to "speed up" my process, I have realized that py2neo is fairly slow so I'm trying my luck with some code magic...

From these benchmarks it appears that unwind is the fastest way to do stuff with db so here's my try...:

example = {}
example["name"] = "someName"
example["type"] = "Pet"
example["atr"] = "lel"
petArray = 100 * [example]
print("made my array of dicts", petArray)'unwind $mapEntry as mItem create (n:mItem["type"] {name:mItem["name"], art:mItem["atr"]})', mapEntry=petArray)

Now I'm getting the >

py2neo.database.ClientError: SyntaxError: Invalid input '[': expected an identifier character, whitespace, NodeLabel, a property map, ')' or a relationship pattern (line 1, column 42 (offset: 41))
"unwind $mapEntry as mItem create (n:mItem["type"] {name:mItem["name"], art:mItem["atr"]})"

Would any1 be so kind to help me out with it ?


I think it's not possible to edit the label as property:)'UNWIND $mapEntry AS mItem CREATE (n:Pet {name:mItem['name'], art:mItem['atr']})", mapEntry=petArray)

This is the link from the doc: Parameters - Cypher Manual

But there is a solution if you need to do it dynamically:)
Doc: 11.4. Creating Data - Chapter 11. Node, Relationship and Path Functions and Procedures

CALL apoc.create.node(['Label'], {key:value,…​})


Thank you for both replies!

I've tweaked my code for the moment to reflect this, as I though I will have to split my maps per "type" and batch individual type 1 by 1... I'll read on the CALL apoc part next!

So far I tweaked the code to be this >'''    unwind $mapEntry as mItem 
              CREATE (n:Pet)
              SET n = mItem
              RETURN n'''
       , mapEntry=petArray)

As one line code breaks my brain :- )

No problem, it's always a pleasure:)

This is a solution with APOC plugin:)'''    UNWIND $mapEntry AS mItem 
              CALL apoc.create.node([mItem["type"]], {name:mItem["name"], art:mItem["atr"]})
              YIELD node
              RETURN node'''
       , mapEntry=petArray)
1 Like

Oh my gash thats amazing! Now I gotta read on apoc :- )Thank you!

Wrapping up as the problem has been solved.


1 Like

One last thing, if you can't do it with Cypher, it's maybe possible with APOC:)

Thank you:)