Most functions in Cypher will return null
if the input parameter is null
.
Here is a list of the functions in Cypher, seperated into three different sections: Predicates, Scalar functions and Aggregated functions
Graph
Predicates are boolean functions that return true or false for a given set of input.
They are most commonly used to filter out subgraphs in the WHERE
part of a query.
Tests whether a predicate holds for all element of this collection collection.
Syntax: ALL(identifier in collection WHERE predicate)
Arguments:
Query
START a=node(3), b=node(1) MATCH p=a-[*1..3]->b WHERE all(x in nodes(p) WHERE x.age > 30) RETURN p
All nodes in the returned paths will have an age
property of at least 30.
Result
p |
---|
1 row |
0 ms |
|
Tests whether a predicate holds for at least one element in the collection.
Syntax: ANY(identifier in collection WHERE predicate)
Arguments:
Query
START a=node(2) WHERE any(x in a.array WHERE x = "one") RETURN a
All nodes in the returned paths has at least one one
value set in the array property named array
.
Returns true if the predicate holds for no element in the collection.
Syntax: NONE(identifier in collection WHERE predicate)
Arguments:
Query
START n=node(3) MATCH p=n-[*1..3]->b WHERE NONE(x in nodes(p) WHERE x.age = 25) RETURN p
No nodes in the returned paths has a age
property set to 25
.
Result
p |
---|
2 rows |
0 ms |
|
|
Returns true if the predicate holds for exactly one of the elements in the collection.
Syntax: SINGLE(identifier in collection WHERE predicate)
Arguments:
Query
START n=node(3) MATCH p=n-->b WHERE SINGLE(var in nodes(p) WHERE var.eyes = "blue") RETURN p
Exactly one node in every returned path will have the eyes
property set to "blue"
.
Result
p |
---|
1 row |
0 ms |
|
Scalar functions return a single value.
To return or filter on the length of a collection, use the LENGTH()
function.
Syntax: LENGTH( collection )
Arguments:
Query
START a=node(3) MATCH p=a-->b-->c RETURN length(p)
The length of the path p
is returned by the query.
Returns a string representation of the relationship type.
Syntax: TYPE( relationship )
Arguments:
Query
START n=node(3) MATCH (n)-[r]->() RETURN type(r)
The relationship type of r
is returned by the query.
Returns the id of the relationship or node.
Syntax: ID( property-container )
Arguments:
Query
START a=node(3, 4, 5) RETURN ID(a)
This returns the node id for three nodes.
Returns the first non-null
value in the list of expressions passed to it.
Syntax: COALESCE( expression [, expression]* )
Arguments:
Query
START a=node(3) RETURN coalesce(a.hairColour?, a.eyes?)
HEAD
returns the first element in a collection.
Syntax: HEAD( expression )
Arguments:
Query
START a=node(2) RETURN a.array, head(a.array)
The first node in the path is returned.
LAST
returns the last element in a collection.
Syntax: LAST( expression )
Arguments:
Query
START a=node(2) RETURN a.array, last(a.array)
The last node in the path is returned.
Collection functions return collections of things — nodes in a path, and so on.
Returns all nodes in a path.
Syntax: NODES( path )
Arguments:
Query
START a=node(3), c=node(2) MATCH p=a-->b-->c RETURN NODES(p)
All the nodes in the path p
are returned by the example query.
Result
NODES(p) |
---|
1 row |
0 ms |
|
Returns all relationships in a path.
Syntax: RELATIONSHIPS( path )
Arguments:
Query
START a=node(3), c=node(2) MATCH p=a-->b-->c RETURN RELATIONSHIPS(p)
All the relationships in the path p
are returned.
To return a single property, or the value of a function from a collection of nodes or relationships,
you can use EXTRACT
. It will go through a collection, run an expression on every element, and return the results
in an collection with these values. It works like the map
method in functional languages such as Lisp and Scala.
Syntax: EXTRACT( identifier in collection : expression )
Arguments:
Query
START a=node(3), b=node(4), c=node(1) MATCH p=a-->b-->c RETURN extract(n in nodes(p) : n.age)
The age property of all nodes in the path are returned.
FILTER
returns all the elements in a collection that comply to a predicate.
Syntax: FILTER(identifier in collection : predicate)
Arguments:
Query
START a=node(2) RETURN a.array, filter(x in a.array : length(x) = 3)
This returns the property named array
and a list of values in it, which have the length 3
.
TAIL
returns all but the first element in a collection.
Syntax: TAIL( expression )
Arguments:
Query
START a=node(2) RETURN a.array, tail(a.array)
This returns the property named array
and all elements of that property except the first one.
Returns numerical values in a range with a non-zero step value step. Range is inclusive in both ends.
Syntax: RANGE( start, end [, step] )
Arguments:
Query
START n=node(1) RETURN range(0,10), range(2,18,3)
Two lists of numbers are returned.
These functions all operate on numerical expressions only, and will return an error if used on any other values.
ABS
returns the absolute value of a number.
Syntax: ABS( expression )
Arguments:
Query
START a=node(3), c=node(2) RETURN a.age, c.age, abs(a.age - c.age)
The absolute value of the age difference is returned.
ROUND
returns the numerical expression, rounded to the nearest integer.
Syntax: ROUND( expression )
Arguments:
Query
START a=node(1) RETURN round(3.141592)
SQRT
returns the square root of a number.
Syntax: SQRT( expression )
Arguments:
Query
START a=node(1) RETURN sqrt(256)
Copyright © 2012 Neo Technology