Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-17-2022 07:48 AM
I'm trying to check if a condition is met to return the desired node and if not met to not return anything. But in my case when the condition is not met a "null" node is returned (probably as expected).
I'm trying to see if a (user1) has "protected = false", then return the node, or else if "protected = true AND user2 [: FOLLOWS] user1" then return the node, other than that I don't want to return anything.
My cypher query is like this.
MATCH (u: User)-[r:CREATED]->(p:Post), (u2: User)
CALL apoc.case([
u.protected = false, 'RETURN p',
u.protected = true AND EXISTS((u2)-[:FOLLOWS]->(u)), 'RETURN p'
],
'',
{p:p, u:u, u2:u2}) YIELD value
RETURN value.p
When a user has "protected = true" and is not followed by user2 then it returns a NULL node. As I said before the returned NULL node might be the expected behavior but I'm asking- Is there a way to tweak this query, even more, to make it not return a NULL node when the condition is not met? OR is this a limitation of this specific procedure? Also is there a different way to implement this query? apoc.when also is not suited for this case. Is there a different way?
Thank you.
Solved! Go to Solution.
10-19-2022 04:35 PM
That makes a lot more sense. Try this:
MATCH (u:User)-[:CREATED]->(p:Post)
WHERE u.protected = false
OR (u.protected = true and EXISTS((:User)-[:FOLLOWS]->(u)))
RETURN p
10-17-2022 10:21 PM
Try this:
MATCH (u: User)-[r:CREATED]->(p:Post)
OPTIONAL MATCH (u2:User)-[:FOLLOWS]->(u)
WHERE id(u2) <> id(u)
WITH COALESCE(u2) as u21, u, p
CALL
{
WITH u21, u, p
WITH u21, u, p where u.protected = false
RETURN p
}
CALL
{
WITH u21, u, p
WITH u21, u, p where u.protected = true and u21 is not null
RETURN p
}
return p
Try this:
10-18-2022 05:50 PM - edited 10-18-2022 05:54 PM
When I run this query I get this error.
Variable `p` already declared in outer scope (line 12, column 10 (offset: 211))
" RETURN p"
Thank you for your response.
10-18-2022 10:10 PM
Try this:
MATCH (u: User)-[r:CREATED]->(p:Post)
OPTIONAL MATCH (u2:User)-[:FOLLOWS]->(u)
WHERE id(u2) <> id(u)
WITH COALESCE(u2) as u21, u, p
CALL apoc.case([
u.protected = false, 'RETURN p',
u.protected = true AND u21 is not null, 'RETURN p'
],
'',
{p:p, u:u, u2:u2}) YIELD value
RETURN value.p
10-19-2022 12:06 AM - edited 10-19-2022 12:10 AM
Still returning the same result with a NULL when user is protected.
By the way in the query above the line
{p:p, u:u, u2:u2}) YIELD value
should have u21 instead of u2.
10-19-2022 10:47 AM
Yes, it should be u21.
10-19-2022 11:39 AM
But still, it is returning the same list with a NULL object.
10-19-2022 04:13 PM
I am not sure I fully understand the requirements, but I came up with this. The query finds all users that have a relationship to a Post. It then returns the post or the string 'no result' if neither of the conditions is satisfied.
MATCH (u:User)-[:CREATED]->(p:Post)
RETURN
CASE
WHEN u.protected = false THEN p
WHEN u.protected = true and EXISTS((:User)-[:FOLLOWS]->(u)) THEN p
ELSE 'no result'
END as result
10-19-2022 04:21 PM
Your query also works.. but the issue here is that if the condition is not met it will return the "no result", which it does in my case. But what I'm looking for is that it doesn't return the "no result" string, I don't want it to return anything if the condition is not met, only when the condition is met do I want to return the node.
So if a list of 10 nodes has one node which doesn't meet the condition then return the list with 9 nodes only and the "no result" one not to be returned. So the list in this case will be clean of any unwanted data.
And thank you for responding @glilienfield.
10-19-2022 04:35 PM
That makes a lot more sense. Try this:
MATCH (u:User)-[:CREATED]->(p:Post)
WHERE u.protected = false
OR (u.protected = true and EXISTS((:User)-[:FOLLOWS]->(u)))
RETURN p
10-19-2022 04:52 PM
SIMPLE AND EFFECTIVE. Your approach worked like a charm.
Thank you so much @glilienfield for taking the time to respond, your help is much appreciated.
Best regards.
All the sessions of the conference are now available online