16.2. Scalar functions

16.2.1. LENGTH
16.2.2. TYPE
16.2.3. ID
16.2.4. COALESCE
16.2.5. HEAD
16.2.6. LAST
16.2.7. TIMESTAMP
16.2.8. STARTNODE
16.2.9. ENDNODE

Scalar functions return a single value.

Figure 16.2. Graph


16.2.1. LENGTH

To return or filter on the length of a collection, use the LENGTH() function.

Syntax: LENGTH( collection )

Arguments:

  • collection: An expression that returns a collection

Query. 

MATCH p=a-->b-->c
WHERE a.name='Alice'
RETURN length(p)

The length of the path p is returned by the query.

Result

length(p)
3 rows

2

2

2


16.2.2. TYPE

Returns a string representation of the relationship type.

Syntax: TYPE( relationship )

Arguments:

  • relationship: A relationship.

Query. 

MATCH (n)-[r]->()
WHERE n.name='Alice'
RETURN type(r)

The relationship type of r is returned by the query.

Result

type(r)
2 rows

"KNOWS"

"KNOWS"


16.2.3. ID

Returns the id of the relationship or node.

Syntax: ID( property-container )

Arguments:

  • property-container: A node or a relationship.

Query. 

MATCH a
RETURN ID(a)

This returns the node id for three nodes.

Result

ID(a)
5 rows

1

2

3

4

5


16.2.4. COALESCE

Returns the first non-null value in the list of expressions passed to it.

Syntax: COALESCE( expression [, expression]* )

Arguments:

  • expression: The expression that might return null.

Query. 

MATCH a
WHERE a.name='Alice'
RETURN coalesce(a.hairColour, a.eyes)

Result

coalesce(a.hairColour, a.eyes)
1 row

"brown"


16.2.5. HEAD

HEAD returns the first element in a collection.

Syntax: HEAD( expression )

Arguments:

  • expression: This expression should return a collection of some kind.

Query. 

MATCH a
WHERE a.name='Eskil'
RETURN a.array, head(a.array)

The first node in the path is returned.

Result

a.arrayhead(a.array)
1 row

["one","two","three"]

"one"


16.2.6. LAST

LAST returns the last element in a collection.

Syntax: LAST( expression )

Arguments:

  • expression: This expression should return a collection of some kind.

Query. 

MATCH a
WHERE a.name='Eskil'
RETURN a.array, last(a.array)

The last node in the path is returned.

Result

a.arraylast(a.array)
1 row

["one","two","three"]

"three"


16.2.7. TIMESTAMP

TIMESTAMP returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. It will return the same value during the whole one query, even if the query is a long running one.

Syntax: TIMESTAMP()

Arguments:

Query. 

START n=node(1)
RETURN timestamp()

The time in milliseconds.

Result

timestamp()
1 row

1375810546999


16.2.8. STARTNODE

STARTNODE returns the starting node of a relationship

Syntax: STARTNODE( relationship )

Arguments:

  • relationship: An expression that returns a relationship

Query. 

MATCH (x:foo)-[r]-()
RETURN startNode(r)

Result

startNode(r)
2 rows

Node[3]{name:"Alice",age:38,eyes:"brown"}

Node[3]{name:"Alice",age:38,eyes:"brown"}


16.2.9. ENDNODE

ENDNODE returns the end node of a relationship

Syntax: ENDNODE( relationship )

Arguments:

  • relationship: An expression that returns a relationship

Query. 

MATCH (x:foo)-[r]-()
RETURN endNode(r)

Result

endNode(r)
2 rows

Node[4]{name:"Bob",age:25,eyes:"blue"}

Node[5]{name:"Charlie",age:53,eyes:"green"}