Search code examples
neo4jcypher

Neo4j Docs provide an example with the incorrect syntax. Trying to use COUNT with the WITH statement


I'm going through the neo4j docs studying cypher's syntax https://neo4j.com/docs/cypher-manual/current/syntax/expressions/#cypher-subquery-expressions

and about half way down the page is this example:

MATCH (person:Person)
WHERE COUNT {
    WITH "Ozzy" AS dogName
    MATCH (person)-[:HAS_DOG]->(d:Dog)
    WHERE d.name = dogName
} = 1
RETURN person.name AS name

and now I want to convert it to run in my sample graph which as nodes PLAYER and TEAM, and relationship PLAYED_AGAINST. My converted query is this:

MATCH (p:PLAYER)
WHERE COUNT {
    WITH "Brooklyn Nets" AS teamName
    MATCH (p)-[:PLAYED_AGAINST]->(t:TEAM)
    WHERE t.name = teamName
} = 1
RETURN p.name AS name

which I think matches neo4j's docs exactly, however, I'm getting this error:

Invalid input 'WITH': expected "(", "allShortestPaths" or "shortestPath" (line 3, column 5 (offset: 35)) " WITH "Brooklyn Nets" AS teamName" ^

which actually looks like this: enter image description here

  1. Are the docs incorrect or am I missing something?

  2. What would the correct syntax be for this?

Thank you.


Solution

  • The most likely reason you are getting this error is that you are not using of the later version of Neo4j. Probably all version of Neo4j v5 work with this syntax, and perhaps some of the later versions of v4, but probably not many. I have both of these queries and they work in Neo4j v5.5.0