15.6. Where

15.6.1. Boolean operations
15.6.2. Filter on node property
15.6.3. Regular expressions
15.6.4. Escaping in regular expressions
15.6.5. Case insensitive regular expressions
15.6.6. Filtering on relationship type
15.6.7. Property exists
15.6.8. Compare if property exists
15.6.9. Filter on null values
15.6.10. Filter on relationships

If you need filtering apart from the pattern of the data that you are looking for, you can add clauses in the where part of the query.

Graph

cypher-where-graph.svg

15.6.1. Boolean operations

You can use the expected boolean operators AND and OR, and also the boolean function NOT().

Query

START n=node(3, 1)
WHERE (n.age < 30 and n.name = "Tobias") or not(n.name = "Tobias")
RETURN n

The node.

Result

n
2 rows, 0 ms

Node[3]{name->"Andres",age->36,belt->"white"}

Node[1]{name->"Tobias",age->25}


15.6.2. Filter on node property

To filter on a property, write your clause after the WHERE keyword.

Query

START n=node(3, 1)
WHERE n.age < 30
RETURN n

The node.

Result

n
1 rows, 1 ms

Node[1]{name->"Tobias",age->25}


15.6.3. Regular expressions

You can match on regular expressions by using =~ /regexp/, like this:

Query

START n=node(3, 1)
WHERE n.name =~ /Tob.*/
RETURN n

The node named Tobias.

Result

n
1 rows, 0 ms

Node[1]{name->"Tobias",age->25}


15.6.4. Escaping in regular expressions

If you need a forward slash inside of your regular expression, escape it just like you expect to.

Query

START n=node(3, 1)
WHERE n.name =~ /Some\/thing/
RETURN n

No nodes match this regular expression.

Result

n
0 rows, 0 ms

(empty result)


15.6.5. Case insensitive regular expressions

By pre-pending a regular expression with (?i), the whole expression becomes case insensitive.

Query

START n=node(3, 1)
WHERE n.name =~ /(?i)ANDR.*/
RETURN n

The node with name Andres is returned.

Result

n
1 rows, 1 ms

Node[3]{name->"Andres",age->36,belt->"white"}


15.6.6. Filtering on relationship type

You can put the exact relationship type in the MATCH pattern, but sometimes you want to be able to do more advanced filtering on the type. You can use the special property TYPE to compare the type with something else. In this example, the query does a regular expression comparison with the name of the relationship type.

Query

START n=node(3)
MATCH (n)-[r]->()
WHERE type(r) =~ /K.*/
RETURN r

The relationship that has a type whose name starts with K.

Result

r
2 rows, 1 ms

:KNOWS[0] {}

:KNOWS[1] {}


15.6.7. Property exists

To only include nodes/relationships that have a property, just write out the identifier and the property you expect it to have.

Query

START n=node(3, 1)
WHERE n.belt
RETURN n

The node named Andres.

Result

n
1 rows, 0 ms

Node[3]{name->"Andres",age->36,belt->"white"}


15.6.8. Compare if property exists

If you want to compare a property on a graph element, but only if it exists, use the nullable property syntax. It is the property with the dot notation, followed by a question mark

Query

START n=node(3, 1)
WHERE n.belt? = 'white'
RETURN n

All nodes, even those without the belt property

Result

n
2 rows, 0 ms

Node[3]{name->"Andres",age->36,belt->"white"}

Node[1]{name->"Tobias",age->25}


15.6.9. Filter on null values

Sometimes you might want to test if a value or an identifier is null. This is done just like SQL does it, with IS NULL. Also like SQL, the negative is IS NOT NULL, althought NOT(IS NULL x) also works.

Query

START a=node(1), b=node(3, 2)
MATCH a<-[r?]-b
WHERE r is null
RETURN b

Nodes that Tobias is not connected to

Result

b
1 rows, 1 ms

Node[2]{name->"Peter",age->34}


15.6.10. Filter on relationships

To filter out subgraphs based on relationships between nodes, you use a limited part of the iconigraphy in the match clause. You can only describe the relationship with direction and eventual type. These are all valid expressions: WHERE a-→b WHERE a←-b WHERE a←[:KNOWS]-b WHERE a-[:KNOWS]-b

      Not that you can not introduce new identifiers here. Although it might look very similar to the MATCH clause, the
WHERE clause is all about eliminating matched subgraphs. MATCH a-->b is very different from WHERE a-->b; the first will
produce a subgraph for every relationship between a and b, and the latter will eliminate any matched subgraphs where a and b
do not have a relationship between them.

Query

START a=node(1), b=node(3, 2)
WHERE a<--b
RETURN b

Nodes that Tobias is not connected to

Result

b
1 rows, 0 ms

Node[3]{name->"Andres",age->36,belt->"white"}