cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

LIMIT 6 returns 7?

In the Querying with Cypher course, in "Limiting the number of intermediate results" (Controlling Results Returned - Querying with Cypher in Neo4j 4.x), the query specifies LIMIT 6 in the WITH clause. However, in the results posted and what I got experimenting in the Neo4j browser myself, 7 nodes are returned: "Emil Eifrem", "Lawrence Fishburne", "Hugo Weaving", "Carrie-Anne Moss", "Keanu Reeves", "The Matrix", and "The Matrix Reloaded".

Can anyone help me understand what is at play here to make things work this way? Thanks!

1 ACCEPTED SOLUTION

Hello @emilgerth


Here is the query:


MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WITH m, p LIMIT 6
RETURN collect(p.name), m.title


It is placing a limit on the number of Person nodes processed. Once it reaches that limit, the query ends. It returns 6 Person nodes and 2 Movie Nodes





Elaine

View solution in original post

3 REPLIES 3

Hello @emilgerth


Here is the query:


MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WITH m, p LIMIT 6
RETURN collect(p.name), m.title


It is placing a limit on the number of Person nodes processed. Once it reaches that limit, the query ends. It returns 6 Person nodes and 2 Movie Nodes





Elaine

Ah ha! So the LIMIT only applies to p in that statement. Thanks so much, that makes way more sense.

Almost. LIMIT applies to the number of rows, not necessarily the number of nodes.

You have WITH m, p LIMIT 6, so you will get 6 rows, each row containing an m and a p. It may help to visualize it by returning at that point (a RETURN instead of a WITH on that line, and omit the rest).

After the result rows are limited, the collection happens, where, per m.title, you collect the names of the associated p nodes.