Check if node exists - proposal for a new APOC-function

@michael.hunger

Unfortunately, I'm at the stage of where Cypher is like Legos: I can build fragments but I can't always build an entire structure that I want. I'm beginning to understand subqueries (which I believe don't get enough emphasis) but I'm not fully there... I don't understand why I'm not allowed to put certain pieces together in certain ways that I would have thought possible.

I naively tried this:

RETURN exists { MATCH (u:User {name: "abc@xyz.at"}) }

but I got this error msg...

The EXISTS subclause is not valid inside a WITH or RETURN clause. (line 1, column 9 (offset: 8))
"RETURN exists {MATCH (u:User {name: "abc@xyz.at"})}"
^

I do see the exist subquery documentation, but it doesn't match up with what I (and the OP) would like to do.

I also tried this: I'd like to do an existing test within apoc.when but this doesn't seem to work either. It seems that Cypher is insisting on having a relationship pattern but I just want to test for existence of a node with a certain property's value.

RETURN
apoc.do.when(exists {(u:User {name: "abc@xyz.at"})}, "RETURN true", "RETURN false") 

Invalid input '}': expected whitespace or a relationship pattern (line 2, column 67 (offset: 73))
"apoc.do.when(exists {MATCH(n) WHERE (u:User {name: "abc@xyz.at"})}, "RETURN true", "RETURN false")"

We did come up with a few alternatives to using EXISTS of a property here:

The documentation for exists() function doesn't have a pointer to exists subqueries:

Sorry for my confusion... I do think if there was an easy way to test for property-with-a-value existence, it would be helpful.

1 Like