Abhishek Pankar
Abhishek Pankar

Reputation: 743

Return data if relationship doesn't exists between the two nodes Neo4j

There are two Nodes with label

  1. Order
  2. Status (By default 5 Status Nodes are created)
    • {code: START}
    • {code: PLACED}
    • {code: DISPATCHED}
    • {code: DELIVERED}
    • {code: COMPLETED}

There exists a relationship between two nodes "HAS_STATUS". When the order proceeds I've made a relation of order with the respective status.

        (start)      (placed)
           Status  Status
               \   /
                \ / 
     Status----Order1------Status
(dispatched)     |           (delivered)
                 |
               Status 
             (completed)



        (start)      (placed)
           Status  Status
               \   /
                \ / 
     Status----Order2------Status
(dispatched)               (delivered)
                 

I want to execute 2 queries:

  1. Get all orders which are completed
  2. Get all order which are not completed

For the first one it returns Order1 as expected:

Match (order:Order)-[:HAS_STATUS]-(status:Status) where status.code="COMPLETED" return order{.*}

For the second it returns Order1 and Order2 (It should return only Order2)

Match (order:Order)-[:HAS_STATUS]-(status:Status) where not status.code="COMPLETED" return order{.*}

Please help me on the second one. I hope my explaination made you clear about the scenario.

Thanks!

Upvotes: 0

Views: 269

Answers (1)

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6514

You could use the following two queries:

  1. query:

    Match (order:Order) WHERE (order)-[:HAS_STATUS]-(:Status{code:"COMPLETED"}) return order{.*}

  2. query:

    Match (order:Order) WHERE NOT (order)-[:HAS_STATUS]-(:Status{code:"COMPLETED"}) return order{.*}

Upvotes: 1

Related Questions