Aviran Katz
Aviran Katz

Reputation: 761

Aggregate nodes and relationships

I'd like to perform a check on a property that exists on both nodes and relationships in my DB. Is there any way to aggregate them all as entity and then perform the check on entity instead of performing three separate checks?

What I currently have is:

MATCH (n1)-[r]-(n2) WHERE (myConditions) 
WITH n1, r, n2 
WHERE n1.property=1 AND r.property=1 AND n2.property=1 
RETURN *

What I'm looking for is something like:

MATCH (n1)-[r]-(n2) WHERE (myConditions) 
WITH n1, r, n2 AS entity
WHERE entity.property=1 
RETURN *

Important note: There are queries with more than 2 nodes and more than one relationships. I would like to aggregate all graph entities and then perform a single check.

By the way, if "aggregation" is not the right term for this case, please feel free to correct me.

Upvotes: 0

Views: 393

Answers (1)

Martin Preusse
Martin Preusse

Reputation: 9369

You could use predicates like all(), any(), none() etc. on all nodes and relationships in your path. You still have to check for nodes and relationships separately.

MATCH path=(n1)-[r]-(n2)
WHERE (myConditions)
WITH path   
WHERE all(n in nodes(path) WHERE n.property = 1)
AND all(r in relationships(path) WHERE r.property = 1)
RETURN path

Upvotes: 2

Related Questions