Neo4j: Cypher - Neo.ClientError.Statement.ParameterMissing and neo4j-shell
Every now and then I get sent Neo4j cypher queries to look at and more often than not they’re parameterised which means you can’t easily run them in the Neo4j browser.
For example let’s say we have a database which has a user called 'Mark':
CREATE (u:User {name: "Mark"})
Now we write a query to find 'Mark' with the name parameterised so we can easily search for a different user in future:
MATCH (u:User {name: {name}}) RETURN u
If we run that query in the Neo4j browser we’ll get this error:
Expected a parameter named name
Neo.ClientError.Statement.ParameterMissing
If we try that in neo4j-shell we’ll get the same exception to start with:
$ MATCH (u:User {name: {name}}) RETURN u;
ParameterNotFoundException: Expected a parameter named name
However, as Michael pointed out to me, the neat thing about neo4j-shell is that we can define parameters by using the export command:
$ export name="Mark"
$ MATCH (u:User {name: {name}}) RETURN u;
+-------------------------+
| u |
+-------------------------+
| Node[1923]{name:"Mark"} |
+-------------------------+
1 row
export is a bit sensitive to spaces so it’s best to keep them to a minimum. e.g. the following tries to create the variable 'name ' which is invalid:
$ export name = "Mark"
name is no valid variable name. May only contain alphanumeric characters and underscores.
The variables we create in the shell don’t have to only be primitives. We can create maps too:
$ export params={ name: "Mark" }
$ MATCH (u:User {name: {params}.name}) RETURN u;
+-------------------------+
| u |
+-------------------------+
| Node[1923]{name:"Mark"} |
+-------------------------+
1 row
A simple tip but one that saves me from having to rewrite queries all the time!
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.