Rahul
Rahul

Reputation: 687

Filtering pattern in cypher query

I was trying to find all the AAA nodes that have Status=Pending and don't have a RELATIONSHIP_1 relationship with BBB node.

I wrote 3 queries which should have returned the same count(according to my understanding):

Match (a:AAA)-[tempRel]->(temp) where a.Status='Pending' AND TYPE(tempRel)<>'RELATIONSHIP_1' RETURN count(DISTINCT a)

Match (a:AAA)-[tempRel]->(temp:BBB) where a.Status='Pending' AND TYPE(tempRel)<>'RELATIONSHIP_1' RETURN count(DISTINCT a)


Match (a:AAA) where a.Status='Pending' AND NOT((a)-[:RELATIONSHIP_1]->(:BBB)) RETURN count(DISTINCT a)

But all the queries returned different results.

Any ideas what I might be missing here?

Regards,

Rahul

Upvotes: 1

Views: 104

Answers (1)

cybersam
cybersam

Reputation: 67044

  1. The following matches any Pending AAA that has a non-RELATIONSHIP_1 relationship to any node (with any label or no label). Note: a matching Pending AAA node could nevertheless also have a separate RELATIONSHIP_1 relationship.

    MATCH (a:AAA)-[tempRel]->(temp)
    WHERE a.Status='Pending' AND TYPE(tempRel)<>'RELATIONSHIP_1'
    RETURN count(DISTINCT a)
    
  2. The following matches any Pending AAA that has a non-RELATIONSHIP_1 relationship to a BBB node. Note: a matching Pending AAA node could nevertheless also have a separate RELATIONSHIP_1 relationship to a BBB node.

    MATCH (a:AAA)-[tempRel]->(temp:BBB)
    WHERE a.Status='Pending' AND TYPE(tempRel)<>'RELATIONSHIP_1'
    RETURN count(DISTINCT a)
    
  3. The following matches any Pending AAA that does not have ANY RELATIONSHIP_1 relationships to a BBB node. This includes Pending AAA nodes that do not have any relationships at all.

    MATCH (a:AAA)
    WHERE a.Status='Pending' AND NOT((a)-[:RELATIONSHIP_1]->(:BBB))
    RETURN count(DISTINCT a)
    

Upvotes: 2

Related Questions