简体   繁体   中英

Neo4j Cypher - Match relationship condition or null

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.

模型

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM