Functions
Aggregate functions
Create the data to be used in the example.
CREATE (:person {name: 'Elsa', age: 20});
CREATE (:person {name: 'Jason', age: 30});
CREATE (:person {name: 'James', age: 40});
CREATE (:person {name: 'Daniel', age: 50});
avg()
Returns the average of numeric values.MATCH (v:person) RETURN avg(v.age);
collect()
Returns a list containing the values returned by the expression; aggregates data by merging multiple records or values into a single list.MATCH (v:Person) RETURN collect(v.age);
count()
Prints the number of result rows; able to print the number or properties of vertices and edges and can be given an alias.MATCH (v:person) RETURN count(v); MATCH (v:person)-[k:knows]->(p) RETURN count(*); MATCH (v:person) RETURN count(v.name) AS CNT;
min()/max()
Takes the numeric attribute as input, returns the minimum/maximum values to the corresponding column.MATCH (v:person) RETURN max(v.age); MATCH (v:person) RETURN min(v.age);
stDev()
Returns the standard deviation. The stDev function returns the standard deviation of the sample population and must cast the property.MATCH (v:person) RETURN stDev(v.age);
stDevP()
Returns the standard deviation. The stDevP function returns the standard deviation of the sample population and must cast the property.MATCH (v:person) RETURN stDevP(v.age);
sum()
Returns the sum of the numeric values. As it is the sum of the numeric values. The property must be cast.MATCH (v:person) RETURN sum(v.age);
Predicate functions
all()
Returns true if all elements in the list satisfy the condition.RETURN ALL(x in [] WHERE x = 0);
any()
Returns true if at least one element in the list satisfies the condition.RETURN ANY(x in [0] WHERE x = 0);
none()
Returns true if no elements in the list satisfy the condition.RETURN NONE(x in [] WHERE x = 0);
single()
Returns true if a single function satisfies only a condition in the list.RETURN SINGLE(x in [] WHERE x = 0);
Scalar functions
coalesce()
Returns the first non-null value in the list.CREATE (:person {name: 'Jack', job: 'Teacher'}); MATCH (a) WHERE a.name = 'Jack' RETURN coalesce(a.age, a.job);
endNode()
Returns the last node in the relationship.CREATE vlabel Developer; CREATE vlabel language; CREATE elabel be_good_at; CREATE (:Developer {name: 'Jason'})-[:be_good_at]->(:language {name: 'C'}); CREATE (:Developer {name: 'David'})-[:be_good_at]->(:language {name: 'JAVA'}); MATCH (x:Developer)-[r]-() RETURN endNode(r);
head()
Returns the first element in the list.CREATE (:person {name: 'Richard', array: [ 1, 2, 3 ]}); MATCH(a) where a.name = 'Richard' RETURN a.array, head(a.array);
id()
Returns the relationship or id of the node; returns the node id for all nodes specified in the argument.MATCH (a) RETURN id(a);
last()
Returns the last element in the list.MATCH (a) WHERE a.name = 'Richard' RETURN a.array, last(a.array);
length()
Returns the length of a string or path. If you specify the property of a string or a string type as an argument, the number of characters in the string is returned.RETURN length('string'); MATCH (a:person) WHERE length(a.name) > 4 RETURN a.name;
properties()
Converts the arguments to a list of key/value mappings. If the argument is already a key/value mapped list, it is returned unchanged.CREATE (p:Person { name: 'Stefan', city: 'Berlin' }) RETURN properties(p);
startNode()
Returns the starting node of the relationship.MATCH (x:Developer)-[r]-() RETURN startNode(r);
toBoolean()
Converts a string to a boolean.RETURN toBoolean('TRUE'), toBoolean('FALSE');
type()
Returns the elabel of the edge passed as an argument. If the elabel of the edge also inherits another elabel, it returns a parent elabel as well. You should be careful when passing arguments to the type function; when you find an edge that matches the pattern using MATCH clause, assign a variable, and then pass the variable as an argument, the edge itself cannot be passed as an argument to the type function, but must always be passed as a variable.CREATE elabel loves; CREATE (:person {name: 'Adam'})-[:loves]->(:person {name: 'Eve'}); MATCH ({name: 'Adam'})-[r]->({name: 'Eve'}) RETURN type(r);
List functions
keys()
Returns a list containing strings for all attribute names of nodes, relationships, and maps.MATCH (a) WHERE a.name = 'Jack' RETURN keys(a);
labels()
Returns vlabel of the vertex passed as an argument. You should be careful when passing arguments to the label function; when you find a vertex that matches the pattern using MATCH clause, assign a variable, and pass that variable as an argument, the vertex itself cannot be passed as an argument to the label function, but must always be passed as a variable.MATCH (a) WHERE a.name='Jack' RETURN labels(a);
nodes()
Returns a vertex that exists in the path passed as an argument. You should be careful when passing arguments to the nodes function; when you find a path that matches the pattern using MATCH clause, assign a variable, and pass that variable as an argument, the path itself cannot be passed as an argument to the nodes function, but must always be passed as variable. When used with the length function, the number of vertices in the path can be found.MATCH p = (a)-[r]->(b) WHERE a.name = 'Adam' and b.name = 'Eve' RETURN nodes(p); MATCH p = (a)-[r]->(b) WHERE a.name = 'Adam' and b.name = 'Eve' RETURN length(nodes(p));
relationships()
Returns the edges present in the path passed as an argument. You should be careful when passing arguments to the relationships function; when you find a path that matches the pattern using MATCH clause, assign a variable, and pass that variable as an argument, the path itself cannot be passed as an argument to the relationships function, but must always be passed as variable. When used with the count function, the number of edges in the path can be found.MATCH p = (a)-[r]->(b) WHERE a.name = 'Adam' and b.name = 'Eve' RETURN relationships(p); MATCH p = (a)-[r]->(b) WHERE a.name = 'Adam' and b.name = 'Eve' RETURN count(relationships(p));
tail()
Returns a list result that contains all elements except the first element in the list.MATCH (a) WHERE a.name = 'Richard' RETURN a.array, tail(a.array);
Mathematical functions
Number
abs()
Returns a numeric value passed as an argument. It may be passed as a decimal number or as a subtraction. The MATCH clause can be used to find a specific element and pass a subtraction of the properties, which are numeric values, among the properties of the elements.RETURN abs(-3.14); RETURN abs(20-45); MATCH (a {name:'Jack'}), (b {name:'Emily'}) RETURN abs(a.age-b.age);
ceil(), floor(), round()
The ceil function rounds the numeric value passed as an argument to the first decimal place. The floor function returns the numeric value passed as an argument to the first decimal place. The round function rounds the numeric value passed as the argument to the first decimal place.RETURN ceil(3.1); RETURN ceil(1); RETURN ceil(-12.19); RETURN floor(3.1); RETURN floor(1); RETURN floor(-12.19); RETURN round(3.1); RETURN round(3.6); RETURN round(-12.19); RETURN round(-12.79);
rand()
Returns an arbitrary floating-point number between 0 and 1.RETURN rand();
sign()
Returns the sign of the numeric value passed as an argument; returns ‘1’ if the argument passed is positive, ‘-1’ if negative, and ‘0’ if zero.RETURN sign(25); RETURN sign(-19.93); RETURN sign(0);
Logarithmic
log()
Returns the natural logarithm of a number.RETURN log(27);
The natural logarithm of 27 is returned.
log10()
Returns the common logarithm (base 10) of a number.RETURN log10(27);
The common logarithm of 27 is returned.
exp()
The exp function returns a power value to base (e) exponentiated by the numeric value passed as an argument. That is, exp(1) returns e^1≒2.71828182845905, exp(2) returns e^2≒7.38905609893065, and exp(-1), e^-1≒0.367879441171442.RETURN exp(1); RETURN exp(2); RETURN exp(-1);
sqrt()
Returns the square root of the numeric value passed as an argument. The sqrt function cannot pass a negative number as an argument.RETURN sqrt(25);
Trigonometric
sin()/cos()/tan()
The sin, cos, and tan functions return the sine, cosine, and tangent values of the numeric values passed as arguments, respectively. sin(), cos(), and tan() print the values in radians; sind(), cosd(), and tand() are used to print the values in degrees.RETURN sin(0.5); RETURN sin(-1.5); RETURN cos(0); RETURN cos(-1); RETURN tan(0); RETURN tan(15.2);
cot()/asin()/acos()/atan()/atan2()
The cot function returns a cotangent value (inverse of tangent) of the numeric value passed as an argument, the asin function returns an arcsine value (inverse of sine) of the numeric value passed as the argument, and the acos function returns an arccosine value of the numeric value (inverse of cosine). The atan, atan2 functions return the arctangent value (inverse of tangent) of the numeric value passed as an argument. The argument range of the acos function is a numeric value between -1 and 1. atan2 has two arguments in order to make the atan function more granular. Conceptually, atan2(a, b) is equivalent to atan (a/b). However, it is not clear whether atan(-5) is atan(-15/3) or atan (15/(-3)). The trigonometric function requires a definite distinction because the argument is a radian value. Therefore, using atan2 rather than atan makes it more accurate.RETURN cot(1.5); RETURN asin(1); RETURN acos(0.5); RETURN atan(-1); RETURN atan2(-1.5, 1.3);
pi()/degrees()/radians()
The pi function returns pi as a number. The degrees function takes the arguments passed as radians and returns them to degrees. The radians function converts the arguments passed to degrees into radians.RETURN pi(); RETURN degrees(12.3); RETURN degrees(pi()); RETURN radians(180);
String functions
replace()
If the second argument is contained in the first argument, replace the second argument with the third argument.RETURN replace('ABCDEFG', 'C', 'Z'); RETURN replace('ABCDEFG', 'CD', 'Z'); RETURN replace('ABCDEFG', 'C', 'ZX'); RETURN replace('ABCDEFG', 'CD', 'ZXY');
substring()
Prints the first argument from the nth digit (n is the number indicated by the second argument). The third argument indicates how many characters to be printed. If there is no third argument and it is greater than the number of characters in the first argument, it prints to the end.RETURN substring('ABCDEFG', 2); RETURN substring('ABCDEFG', 2, 3); RETURN substring('ABCDEFG', 4, 10);
left()/right()
The left function prints the first argument from the left, and the right function prints characters up to length of n (n is the number indicated by in the second argument) from the right. If the value of the second argument is larger than the number of characters remaining, the number of characters remaining will be printed out.RETURN left('AAABBB', 3); RETURN right('AAABBB', 3);
lTrim()/rTrim()
The lTrim function removes all left whitespace from the passed argument, and the rTrim function removes all right whitespace before printing.RETURN lTrim(' ABCD '); RETURN rTrim(' ABCD ');
toLower()/toUpper()
The toLower function converts all passed arguments to lower case and the toUpper function converts them to upper case.RETURN toLower('AbCdeFG'); RETURN toUpper('AbCdeFG');
reverse()
Prints the arguments in reverse order.RETURN reverse('ABCDEFG');
toString()
Converts an integer, floating, or boolean value to a string.RETURN toString(11.5), toString('already a string'), toString(TRUE);
trim()
Returns the original string with the leading and trailing spaces removed.RETURN trim(' hello ');