Syntax

Parameters

To create parameters in Cypher :

:params name=>"Alec Baldwin"
CREATE (n:ACTOR {name:$name})
RETURN n

An array of properies can be created

:params props=>[{name:'Martin Scorcese', age:70, bornin:'Los Angeles'},
{name:'Eric VanLoock', age:60, bornin:'Antwerp'}]

which result in :

{
"props": [
    {
    "name": "Martin Scorcese",
    "bornin": "Los Angeles",
    "age": 70
    },
    {
    "name": "Eric VanLoock",
    "bornin": "Antwerp",
    "age": 60
    }
    ]
}

This can then be used in Cypher :

UNWIND $props AS properties
CREATE (n:Person)
SET n = properties
RETURN n

Lists

A literal list is created by using brackets and separating the elements in the list with commas.

Example:

RETURN [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] AS list

We can use the range function. It gives us a list containing all numbers between given start and end numbers. Range is inclusive in both ends. To access individual elements in the list, we use the square brackets again. This will extract from the start index and up to but not including the end index.

RETURN range(0, 10)[3]
RETURN range(0, 10)[0..3]
RETURN range(0, 10)[-5..]
RETURN size(range(0, 10)[0..3])

List comprehension is a syntactic construct available in Cypher for creating a list based on existing lists. It follows the form of the mathematical set-builder notation (set comprehension) instead of the use of map and filter functions.

To create the square of all even numbers <= 10:

RETURN [x IN range(0,10) WHERE x % 2 = 0 | x^2 ] AS result

Omitting the expression :

RETURN [x IN range(0,10) WHERE x % 2 = 0 ] AS result

Omitting WHERE :

Pattern comprehension

Pattern comprehension is a syntactic construct available in Cypher for creating a list based on matchings of a pattern. A pattern comprehension will match the specified pattern just like a normal MATCH clause, with predicates just like a normal WHERE clause, but will yield a custom projection as specified.

The following graph is used for the example below:

../_images/patternlist.png
MATCH (a:Person {name: 'Keanu Reeves'})
RETURN [(a)-->(b) WHERE b:Movie | b.released] AS years

Maps

With the following file a mini-graph database for movies can be created:

Can you explain the different outcome for the two code-snippes below ?

  1. MATCH p=()-[r:WROTE]->()
    RETURN p
    
  2. MATCH (p)-[r:WROTE]->()
    RETURN p
    

Find ‘Tom Hanks’ and return data about him and the movies he has acted in. This example shows an example of map projection with a literal entry, which in turn also uses map projection inside the aggregating collect().

MATCH (actor:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(movie:Movie)
RETURN actor{.name, .realName, movies: collect(movie{.title, .year})}

Find all persons that have acted in movies, and show number for each. This example introduces an variable with the count, and uses a variable selector to project the value.

MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)
WITH actor, count(movie) AS nbrOfMovies
RETURN actor{.name, nbrOfMovies}

Create a new kind of node, with the label NewPerson. This node has two properties: the name of the actor and the number of movies he/she acted in.

MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)
WITH actor, count(movie) AS nbrOfMovies
create (p:NewPerson)
SET p=actor{.name, nbrOfMovies}
RETURN p

Again, focusing on ‘Tom Hanks’, this time returning all properties from the node. Here we use an all-properties selector to project all the node properties, and additionally, explicitly project the property age.

MATCH (actor:Person {name: 'Tom Hanks'})
RETURN actor{.*, .age}

Last change: Oct 30, 2023