Reputation: 3225
I have a simple social stream where few nodes and relationships look like the following:
row 1: (a) -> (stream) -> (d)
row 2: (a) -> (stream) -> (d) -> (source)
Basically I want to pull both rows but put some restrictions on the type of source, for example I currently use this:
MATCH (a)-[]->(stream)-[]->(d)
OPTIONAL MATCH (d)-[]->(source)
WHERE source.x = 3
RETURN stream, d, source
This works well. Almost. When source.x = 3, I get both the rows. Which is perfect. But when source.x != 3, I want the query to ignore the second row, but because of optional match the row still appears.
When source.x = 3
row 1. stream, d, null
row 2. stream, d, source
When source.x != 3
row 1. stream, d, null
row 2. stream, d, null
When source.x != 3, I want the query to ignore the second row. Because it contains the (source) node but not the one we want. The output should look like:
row 1. stream, d, null
Basically something like, if (source) does not exist show row.. if it does exist, show row only if source.x = 3
EDIT: As recommended in the comment, I have attached a simplified example.
Upvotes: 0
Views: 48
Reputation: 20185
You can pipe the results with WITH
and a WHERE
clause for filtering before returning them :
MATCH (me:User {name:'me'})-[:LIKES]->(transport)
OPTIONAL MATCH (transport)-[:HAS_DRIVER]->(driver:Driver {name:'Anne'})
WITH me, transport, driver
WHERE (NOT (transport)-[:HAS_DRIVER]->()) OR (NOT driver IS NULL)
RETURN me, transport, driver
Upvotes: 1