Parameters
Cypher statements will often include literal values such as names, dates, or numbers.
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.name = "Tom Hanks"
RETURN m.titleParameters in Cypher
You can use parameters to represent these literal values in your Cypher statements.
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.name = $actorName
RETURN m.title$ prefix
Parameters are prefixed with the$ symbol in the query.Setting parameters
You can set the value for a single parameter using the :param command:
:param actorName => "Tom Hanks"The parameter can then be used in subsequent queries:
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.name = $actorName
RETURN m.titleScope of parameters
Parameters set using the :param command are scoped to your Neo4j Browser session.
Parameters persist until you close the Neo4j Browser or explicitly remove them.
Setting multiple parameters
You can also use the JSON-style syntax to set all of the parameters in your Neo4j Browser session.
:params {actorName: "Tom Cruise", minimumRevenue: 100000000}All the parameters you set using this approach will replace any previously set parameters:
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE
p.name = $actorName AND
m.revenue > $minimumRevenue
RETURN m.titleSpecific types
The parameters types you can specify in using this approach are numbers, strings, and booleans.
Viewing parameters
You can view the parameters currently set in your Neo4j Browser session by using the :params command.
:paramsThe command will return a JSON object showing all the parameters and their current values.
{
"actorName": "Tom Cruise",
"minimumRevenue": 100000000.0
}Performance benefits
Using parameters in your Cypher queries can improve your query performance:
-
Neo4j will cache execution plans for queries that use parameters.
-
The same query with different parameter values will not require a new execution plan to be generated.
Challenges
Create parameters to ensure the following queries run successfully.
-
Find the movies directed by "Buster Keaton" .
cypherMATCH (p:Person)-[:DIRECTED]->(m:Movie) WHERE p.name = $directorName RETURN m.title -
Find movies spoken in ["French"] with an minimum average rating of 4 .
cypherMATCH (m:Movie)<-[r:RATED]-() WITH m, avg(r.rating) as avgRating WHERE $language in m.languages AND avgRating > $minimumRating RETURN m.title, avgRating
Click to reveal the answers
-
Find the movies directed by "Buster Keaton".
cypher:param directorName => "Buster Keaton"cypherMATCH (p:Person)-[:DIRECTED]->(m:Movie) WHERE p.name = $directorName RETURN m.title -
Find movies spoken in "French" with an minimum average rating of 4.
cypher:params {language: "French", minimumRating: 4 }cypherMATCH (m:Movie)<-[r:RATED]-() WITH m, avg(r.rating) as avgRating WHERE $language in m.languages AND avgRating > $minimumRating RETURN m.title, avgRating
Next
Summary
In this lesson, you learned how to set parameter values and to use them in queries.