Using Parameters

Parameters

Cypher statements will often include literal values such as names, dates, or numbers.

cypher
A query with a literal value
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.name = "Tom Hanks"
RETURN m.title

Parameters in Cypher

You can use parameters to represent these literal values in your Cypher statements.

cypher
Parameterized query
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:

cypher
Set the parameter actorName to "Tom Hanks"
:param actorName => "Tom Hanks"

The parameter can then be used in subsequent queries:

cypher
Use the parameter in a query
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE p.name = $actorName
RETURN m.title

Scope 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.

cypher
Set two parameters
:params {actorName: "Tom Cruise", minimumRevenue: 100000000}

All the parameters you set using this approach will replace any previously set parameters:

cypher
Use the parameters in a query
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WHERE
    p.name = $actorName AND
    m.revenue > $minimumRevenue
RETURN m.title

Specific 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.

cypher
View parameters
:params

The command will return a JSON object showing all the parameters and their current values.

json
{
  "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.

  1. Find the movies directed by "Buster Keaton" .

    cypher
    MATCH (p:Person)-[:DIRECTED]->(m:Movie)
    WHERE p.name = $directorName
    RETURN m.title
  2. Find movies spoken in ["French"] with an minimum average rating of 4 .

    cypher
    MATCH (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
  1. Find the movies directed by "Buster Keaton".

    cypher
    :param directorName => "Buster Keaton"
    cypher
    MATCH (p:Person)-[:DIRECTED]->(m:Movie)
    WHERE p.name = $directorName
    RETURN m.title
  2. Find movies spoken in "French" with an minimum average rating of 4.

    cypher
    :params {language: "French", minimumRating: 4 }
    cypher
    MATCH (m:Movie)<-[r:RATED]-()
    WITH m, avg(r.rating) as avgRating
    WHERE
        $language in m.languages AND
        avgRating > $minimumRating
    RETURN m.title, avgRating

Summary

In this lesson, you learned how to set parameter values and to use them in queries.

Chatbot

How can I help you today?