1
votes

A bit of a Neo4j newbie and I've been working on a query that seems like it should be simple, yet I'm having problems figuring out the correct Cypher. Given the below model, I want to get all Objects (and their Statuses) that are replacements for obj1 that either have a Status type of 2, or no Status. My desired result is to return the ob3 node and it's Status node in addition to the ob4 node.

So far, the closest I've come is:

match (obj1:Object{nm:'obj1'})-[:REPLACES*]->(repObj)
optional match p=(repObj)-[:HAS_STATUS]->(stat)
where stat.type = 2
return repObj, p

This returns my desired nodes but also returns the ob2 node without it's Status node. If I keep the above query but only return the p variable, I get the ob3 node and it's Status, but do not get back the ob4 node. I've tried a combination of different queries including WITH, NOT and UNION clauses but I feel like I'm missing something very simple.

Model

1

1 Answers

0
votes

You're close, but we need to filter out nodes that aren't what we're looking for, and that requires a WHERE clause on a MATCH or on a WITH. Your WHERE is on the OPTIONAL MATCH right now, which means it won't filter the row (since OPTIONAL MATCH never filters rows, only sets new variables introduced in the match to null).

match (obj1:Object{nm:'obj1'})-[:REPLACES*]->(repObj)
optional match p=(repObj)-[:HAS_STATUS]->(stat)
with repObj, p, stat
where p is null or stat.type = 2
return repObj, p