Search code examples
python-3.xneo4jneo4j-driver

Neo4j - property as variable to return


I'm using python driver for neo4j (neo4j.v1) and I'm trying to write a generic function that gets node's unique id and a property and returns the value of the property of this node with the unique id.

I have tried using the following code without success :

def prop_by_node_id(tx, node_id, prop):
    prop = "s." + prop
    result = tx.run("MATCH (s) WHERE s.id = $id return $prop", id=node_id, prop=prop)

I serached for a solution in APOC and couldn't find one. Thank you


Solution

  • Labels, relationship types & properties can't be dynamic into a cypher query. So you need to generate your cypher query in Python.

    Moreover, your query will not be performant, because the label is missing on the node s, so you will do a all node scan that is really bad.