Parameters
Introduction
Cypher® supports querying with parameters. This means developers don’t have to resort to string building to create a query. Additionally, parameters make caching of execution plans much easier for Cypher, thus leading to faster query execution times.
Parameters can be used for:
-
literals and expressions
-
node and relationship ids
-
for explicit indexes only: index values and queries
Parameters cannot be used for the following constructs, as these form part of the query structure that is compiled into a query plan:
-
property keys; so,
MATCH (n) WHERE n.$param = 'something'
is invalid -
relationship types
-
labels
Parameters may consist of letters and numbers, and any combination of these, but cannot start with a number or a currency symbol.
For details on using parameters via the Neo4j HTTP API, see the HTTP API documentation.
We provide below a comprehensive list of examples of parameter usage. In these examples, parameters are given in JSON; the exact manner in which they are to be submitted depends upon the driver being used.
It is recommended that the new parameter syntax |
String literal
{
"name" : "Johan"
}
MATCH (n:Person)
WHERE n.name = $name
RETURN n
You can use parameters in this syntax as well:
{
"name" : "Johan"
}
MATCH (n:Person { name: $name })
RETURN n
Regular expression
{
"regex" : ".*h.*"
}
MATCH (n:Person)
WHERE n.name =~ $regex
RETURN n.name
Case-sensitive string pattern matching
{
"name" : "Michael"
}
MATCH (n:Person)
WHERE n.name STARTS WITH $name
RETURN n.name
Create node with properties
{
"props" : {
"name" : "Andy",
"position" : "Developer"
}
}
CREATE ($props)
Create multiple nodes with properties
{
"props" : [ {
"awesome" : true,
"name" : "Andy",
"position" : "Developer"
}, {
"children" : 3,
"name" : "Michael",
"position" : "Developer"
} ]
}
UNWIND $props AS properties
CREATE (n:Person)
SET n = properties
RETURN n
Setting all properties on a node
Note that this will replace all the current properties.
{
"props" : {
"name" : "Andy",
"position" : "Developer"
}
}
MATCH (n:Person)
WHERE n.name='Michaela'
SET n = $props
SKIP
and LIMIT
{
"s" : 1,
"l" : 1
}
MATCH (n:Person)
RETURN n.name
SKIP $s
LIMIT $l
Multiple node ids
{
"ids" : [ 0, 1, 2 ]
}
MATCH (n)
WHERE id(n) IN $ids
RETURN n.name
Calling procedures
{
"indexname" : ":Person(name)"
}
CALL db.resampleIndex($indexname)