Tom
Tom

Reputation: 522

Return if no relationship with a given property exists?

I found this question about checking that no outgoing relationships of a given label exist on a node, and this one for checking the count of outgoing relationships with a given property, but what I need to do is get the nodes which don't have a relationship for which a given property is set. I'm sure I'm making this more difficult for myself!

What I have so far is this:

MATCH (n:Node)-[r:WEIGHTING]->()
WHERE NOT(ANY(rel IN r WHERE EXISTS(r.PROP)))
RETURN z

But, obviously, the r at this point is a single relationship, not the collection of relationships. I think I need to get a WITH clause involved, but I'm very much out of my depth!

How would I go about getting the set of Nodes which have no outgoing WEIGHTING relationships that have a PROP property?

I hope that's enough detail - sorry if it's unclear!

Thanks very much, Tom

Upvotes: 1

Views: 769

Answers (2)

InverseFalcon
InverseFalcon

Reputation: 30397

With Neo4j there tend to be several ways to do what you need. One alternate query could be:

MATCH (n:Node)
// only need the next WHERE clause if a WEIGHTING relationship is required
WHERE SIZE((n)-[:WEIGHTING]->()) > 0
OPTIONAL MATCH (n)-[r:WEIGHTING]->()
WHERE EXISTS (r.PROP)
WITH n 
WHERE r is null
RETURN n

Upvotes: 1

Christophe Willemsen
Christophe Willemsen

Reputation: 20175

You can achieve it like this :

MATCH (n:Node)-[r:WEIGHTING]->()
WITH n, collect(r.PROP) AS props
WHERE size(props) = 0
RETURN n

Or :

MATCH (n:Node)-[r:WEIGHTING]->()
WITH n, collect(r) AS rs
WHERE NONE ( x IN rs WHERE EXISTS(x.PROP) )
RETURN n

Upvotes: 1

Related Questions