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

Hello,

I would like to suggest that in the next version of APOC a new function could be implemented which checks, if a certain node with a specific value in a property exists or not.

For example:

apoc.node.exist(u:User {user_email: "abc@xyz.at"})

The result could be a very simple TRUE / FALSE.

Thank you,

JJJ

Why not just use cypher matching? Or existential subqueries?

WHERE exists { MATCH (u:User {user_email: "abc@xyz.at"}) }

Thank you Michael, two users of the community (I don't know how to mention them here) have already found a solution in another thread (I don't know how to link there).

It was in this thread:

1 Like

I think it's a little trickier than that.

This does NOT work (although it seems like it should):
RETURN exists (MATCH (u:User {name: "abc@xyz.at"}))

Invalid input ')': expected whitespace or a relationship pattern (line 1, column 55 (offset: 54))
"RETURN  exists (MATCH (u:User {name: "abc@xyz.at"}))"
                                                       ^

In other words, this suggestion would be satisfied if exists()function was expanded to take a larger types of arguments.

1 Like

Curly braces. Not parentheses

Michael, could you please outline the difference between exists() and exists {}?

THX, JJJ

exits() is a predicate function, which might be deprecated at some point.

And exists {} is more like a structural clause, also called existential subquery, so like a subquery but one that only returns false/true on zero/non-zero results of the query (which currently can contain match/where)

@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

Hi,
Just tried this and it didn't work.

Got the following error:

Invalid input 'WHERE': expected 
  "RETURN"
  "CREATE"
  "DELETE"
  "SET"
  "REMOVE"
  "DETACH"
  "MATCH"
  "WITH"
  "UNWIND"
  "USE"
  "CALL"
  "LOAD"
  "FROM"
  "FOREACH"
  "MERGE"
  "OPTIONAL"
  "USING" (line 1, column 1 (offset: 0))
"WHERE exists { MATCH (u:Client{code:"12"}) }"
 ^

Neo4j 4.2.1

Thanks