Query parameters

This section contains a list of query parameters examples.

For more information on parameters see the Neo4j Cypher Manual.

The examples below illustrate how to use parameters when executing Cypher queries from Java.

Node ID:

Map<String,Object> params = new HashMap<>();
params.put( "id", 0 );

String query =
    "MATCH (n)" + "\n" +
    "WHERE id(n) = $id" + "\n" +
    "RETURN n.name";

Result result = transaction.execute( query, params );

Node object:

Map<String,Object> params = new HashMap<>();
params.put( "node", bobNode );

String query =
    "MATCH (n:Person)" + "\n" +
    "WHERE n = $node" + "\n" +
    "RETURN n.name";

Result result = transaction.execute( query, params );

Multiple node IDs:

Map<String,Object> params = new HashMap<>();
params.put( "ids", asList( 0, 1, 2 ) );

String query =
    "MATCH (n)" + "\n" +
    "WHERE id(n) IN $ids" + "\n" +
    "RETURN n.name";

Result result = transaction.execute( query, params );

String literal:

Map<String,Object> params = new HashMap<>();
params.put( "name", "Johan" );

String query =
    "MATCH (n:Person)" + "\n" +
    "WHERE n.name = $name" + "\n" +
    "RETURN n";

Result result = transaction.execute( query, params );

Numeric parameters for SKIP and LIMIT:

Map<String,Object> params = new HashMap<>();
params.put( "s", 1 );
params.put( "l", 1 );

String query =
    "MATCH (n:Person)" + "\n" +
    "RETURN n.name" + "\n" +
    "SKIP $s" + "\n" +
    "LIMIT $l";

Result result = transaction.execute( query, params );

Regular expression:

Map<String,Object> params = new HashMap<>();
params.put( "regex", ".*h.*" );

String query =
    "MATCH (n:Person)" + "\n" +
    "WHERE n.name =~ $regex" + "\n" +
    "RETURN n.name";

Result result = transaction.execute( query, params );

Create node with properties:

Map<String,Object> props = new HashMap<>();
props.put( "name", "Andy" );
props.put( "position", "Developer" );

Map<String,Object> params = new HashMap<>();
params.put( "props", props );

String query = "CREATE ($props)";

transaction.execute( query, params );

Create multiple nodes with properties:

Map<String,Object> n1 = new HashMap<>();
n1.put( "name", "Andy" );
n1.put( "position", "Developer" );
n1.put( "awesome", true );

Map<String,Object> n2 = new HashMap<>();
n2.put( "name", "Michael" );
n2.put( "position", "Developer" );
n2.put( "children", 3 );

Map<String,Object> params = new HashMap<>();
List<Map<String,Object>> maps = asList( n1, n2 );
params.put( "props", maps );

String query =
    "UNWIND $props AS properties" + "\n" +
    "CREATE (n:Person)" + "\n" +
    "SET n = properties" + "\n" +
    "RETURN n";

Result result = transaction.execute( query, params );

Setting all properties on node:

Map<String, Object> n1 = new HashMap<>();
n1.put( "name", "Andy" );
n1.put( "position", "Developer" );

Map<String, Object> params = new HashMap<>();
params.put( "props", n1 );

String query =
    "MATCH (n:Person)" + "\n" +
    "WHERE n.name = 'Michaela'" + "\n" +
    "SET n = $props";

transaction.execute( query, params );