For example, a case where you want to do a filter, only in the case where the node DOESN'T exist.Here's the wrong way that will give you bad results:
OPTIONAL MATCH (n:Node {id:123})
WHERE n IS NULL
...
Here's the right way:
OPTIONAL MATCH (n:Node {id:123})
WITH n
WHERE n IS NULL
...
Why is that? Because the WHERE clause isn't standalone, it is associated with the preceeding clause. When you have
OPTIONAL MATCH ... WHERE
then it's part of the OPTIONAL MATCH, and as such if the pattern doesn't find any matches, or the WHERE clause fails, no filtering will occur, you'll get a row with null
for any newly introduced variables (remember also that OPTIONAL MATCH can't change or filter any existing variables).OPTIONAL MATCHes don't filter. So to get the WHERE clause to filter out a row, it has to be associated with a regular MATCH or a WHERE clause.
Comments
0 comments
Please sign in to leave a comment.