neo4jcypher

How to achieve the following in Cypher query?


I tried the following query

START s=node(1), t=node(4)
MATCH p=s-[*]-pt--t
WHERE SINGLE (n1 IN nodes(p) 
              WHERE id(n1)=id(t))
WITH DISTINCT pt AS pts, t
MATCH p=t-[*]-pfn
WHERE NONE (n IN nodes(p) 
            WHERE id(n)=3 OR id(n)=7)
RETURN DISTINCT pfn AS pf

but I don't want to hard code 3 and 7 in the penultimate line where 3 and 7 are the nodes contained in (pts). I tried the following but I am getting "Unclosed parenthesis" error

START s=node(1), t=node(4) 
MATCH p=s-[*]-pt--t 
WHERE SINGLE (n1 IN nodes(p) 
              WHERE id(n1)=id(t)) 
WITH DISTINCT pt AS pts, t 
MATCH p=t-[*]-pfn FOREACH(pt in pts : 
                          WHERE NONE (n IN nodes(p) 
                                      WHERE id(n)=id(pt))) 
RETURN DISTINCT pfn AS pf

Solution

  • I think you can use the ALL predicate to ensures that for each node n in the path p there doesn't exist a node in pt that has the same id as the node n,

    START s=node(1), t=node(4) 
    MATCH p=s-[*]-pt--t 
    WHERE SINGLE (n1 IN nodes(p) 
                  WHERE id(n1)=id(t)) 
    WITH DISTINCT collect(id(pt)) AS pts, t 
    MATCH p=t-[*]-pfn 
    WHERE ALL (n IN nodes(p) 
               WHERE NONE (pt IN pts 
                           WHERE id(n)= pt)) 
    RETURN DISTINCT pfn AS pf