3.17. 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) WHERE id(n) = $id RETURN n.name";
        Result result = db.execute( query, params );

Node object:

        Map<String, Object> params = new HashMap<>();
        params.put( "node", bobNode );
        String query = "MATCH (n:Person) WHERE n = $node RETURN n.name";
        Result result = db.execute( query, params );

Multiple node ids:

        Map<String, Object> params = new HashMap<>();
        params.put( "ids", Arrays.asList( 0, 1, 2 ) );
        String query = "MATCH (n) WHERE id(n) IN $ids RETURN n.name";
        Result result = db.execute( query, params );

String literal:

        Map<String, Object> params = new HashMap<>();
        params.put( "name", "Johan" );
        String query = "MATCH (n:Person) WHERE n.name = $name RETURN n";
        Result result = db.execute( query, params );

Index value:

            Map<String, Object> params = new HashMap<>();
            params.put( "value", "Michaela" );
            String query = "START n=node:people(name = $value) RETURN n";
            Result result = db.execute( query, params );

Index query:

            Map<String, Object> params = new HashMap<>();
            params.put( "query", "name:Bob" );
            String query = "START n=node:people($query) RETURN n";
            Result result = db.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) RETURN n.name SKIP $s LIMIT $l";
        Result result = db.execute( query, params );

Regular expression:

        Map<String, Object> params = new HashMap<>();
        params.put( "regex", ".*h.*" );
        String query = "MATCH (n:Person) WHERE n.name =~ $regex RETURN n.name";
        Result result = db.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)";
        db.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 = Arrays.asList( n1, n2 );
        params.put( "props", maps );
        String query = "UNWIND $props AS properties CREATE (n:Person) SET n = properties RETURN n";
        db.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) WHERE n.name='Michaela' SET n = $props";
            db.execute( query, params );