Application Examples Using Parameters

This lesson is intended to give you a very brief view of how a few of the programming languages that Neo4j supports uses Cypher with parameters.

Although each language has its own distinct syntax, the process for each language is similar. Parameters are sent as an additional argument when a Cypher statement is run through the driver, and are referenced in the same way as covered in this module, by applying the $ prefix.

Example: Java

Here is a code snippet that illustrates the use of parameters in Java:

java
try (var session = driver.session()) {
  Result res = session.readTransaction(tx -> tx.run("""
    MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
    WHERE m.title = $title
    RETURN p
    LIMIT 10
  """, Values.parameters("title", "Toy Story")));
}

In this example, the parameters are passed to the second argument of the tx.run method using the static parameters function provided by the org.neo4j.driver.Values class.

For more information on using parameters in Java, check out the Building Neo4j Applications with Java course.

Example: JavaScript

Here is a code snippet that illustrates the use of parameters in JavaScript:

js
const session = driver.session()
const res = await session.readTransaction(tx =>
  tx.run(`
    MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
    WHERE m.title = $title
    RETURN p
    LIMIT 10
  `,
  { title: 'Toy Story'})
)

In this example, the parameters are passed to the second argument of the tx.run method as a JavaScript object.

For more information on using parameters in JavaScript, check out the Building Neo4j Applications with Node.js course.

Example: Python

Here is a code snippet that illustrates the use of parameters in Python:

python
def get_actors(tx, movieTitle): # (1)
  result = tx.run("""
    MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
    WHERE m.title = $title
    RETURN p
  """, title=movieTitle)

  # Access the `p` value from each record
  return [ record["p"] for record in result ]

with driver.session() as session:
    result = session.read_transaction(get_actors, movieTitle="Toy Story")

In Python, Cypher parameters are passed as named parameters to the tx.run method. In this example, title has been passed as a named parameter.

For more information on using parameters in Python, check out the Building Neo4j Applications with Python course.

Check your understanding

What character is used to prefix parameters in a Cypher statement?

Select all that apply:

  • $

  • !

  • &

  • *

Hint

You have seen this special character used in the application examples of this lesson.

Solution

The correct answer is: $.

Summary

In this lesson, you saw a few examples of how different programming languages pass parameters into Cypher queries.

Congratulations! You have completed this course!