15.17. With

15.17.1. Filter on aggregate function results
15.17.2. Sort results before using collect on them
15.17.3. Limit branching of your path search
15.17.4. Alternative syntax of WITH

The ability to chain queries together allows for powerful constructs. In Cypher, the WITH clause is used to pipe the result from one query to the next.

WITH is also used to separate reading from updating of the graph. Every sub-query of a query must be either read-only or write-only.

Figure 15.10. Graph


15.17.1. Filter on aggregate function results

Aggregated results have to pass through a WITH clause to be able to filter on.

Query. 

START david=node(1)
MATCH david--otherPerson-->()
WITH otherPerson, count(*) as foaf
WHERE foaf > 1
RETURN otherPerson

The person connected to David with the at least more than one outgoing relationship will be returned by the query.

Result

otherPerson
0 row
0 ms

(empty result)


Try this query live. (1) {"name":"David"} (2) {"name":"Emil"} (3) {"name":"Anders"} (4) {"name":"Bossman"} (5) {"name":"Cesar"} (1)-[:KNOWS]->(3) {} (3)-[:KNOWS]->(4) {} (3)-[:BLOCKS]->(5) {} (4)-[:KNOWS]->(2) {} (4)-[:BLOCKS]->(1) {} (5)-[:KNOWS]->(2) {} start david=node(1) match david--otherPerson-->() with otherPerson, count(*) as foaf where foaf > 1 return otherPerson

15.17.2. Sort results before using collect on them

You can sort your results before passing them to collect, thus sorting the resulting collection.

Query. 

START n=node(*)
WITH n
ORDER BY n.name desc
LIMIT 3
RETURN collect(n.name)

A list of the names of people in reverse order, limited to 3, in a collection.

Result

collect(n.name)
0 row
0 ms

(empty result)


Try this query live. (1) {"name":"David"} (2) {"name":"Emil"} (3) {"name":"Anders"} (4) {"name":"Bossman"} (5) {"name":"Cesar"} (1)-[:KNOWS]->(3) {} (3)-[:KNOWS]->(4) {} (3)-[:BLOCKS]->(5) {} (4)-[:KNOWS]->(2) {} (4)-[:BLOCKS]->(1) {} (5)-[:KNOWS]->(2) {} start n=node(*) with n order by n.name desc limit 3 return collect(n.name)

15.17.3. Limit branching of your path search

You can match paths, limit to a certain number, and then match again using those paths as a base As well as any number of similar limited searches.

Query. 

START n=node(3)
MATCH n--m
WITH m
ORDER BY m.name desc
LIMIT 1
MATCH m--o
RETURN o.name

Starting at Anders, find all matching nodes, order by name descending and get the top result, then find all the nodes connected to that top result, and return their names.

Result

o.name
0 row
0 ms

(empty result)


Try this query live. (1) {"name":"David"} (2) {"name":"Emil"} (3) {"name":"Anders"} (4) {"name":"Bossman"} (5) {"name":"Cesar"} (1)-[:KNOWS]->(3) {} (3)-[:KNOWS]->(4) {} (3)-[:BLOCKS]->(5) {} (4)-[:KNOWS]->(2) {} (4)-[:BLOCKS]->(1) {} (5)-[:KNOWS]->(2) {} start n=node(3) match n--m with m order by m.name desc limit 1 match m--o return o.name

15.17.4. Alternative syntax of WITH

If you prefer a more visual way of writing your query, you can use equal-signs as delimiters before and after the column list. Use at least three before the column list, and at least three after.

Query. 

START david=node(1)
MATCH david--otherPerson-->()
========== otherPerson, count(*) as foaf ==========
SET otherPerson.connection_count = foaf

For persons connected to David, the connection_count property is set to their number of outgoing relationships.

Result

Properties set: 2
0 ms

(empty result)


Try this query live. (1) {"name":"David"} (2) {"name":"Emil"} (3) {"connection_count":2,"name":"Anders"} (4) {"connection_count":1,"name":"Bossman"} (5) {"name":"Cesar"} (1)-[:KNOWS]->(3) {} (3)-[:KNOWS]->(4) {} (3)-[:BLOCKS]->(5) {} (4)-[:KNOWS]->(2) {} (4)-[:BLOCKS]->(1) {} (5)-[:KNOWS]->(2) {} start david=node(1) match david--otherPerson-->() ========== otherPerson, count(*) as foaf ========== set otherPerson.connection_count = foaf