1

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

Community
  • 1
  • 1
Tom
  • 522
  • 3
  • 13
  • Should this also return nodes that don't have any outgoing WEIGHTING relationships at all, or do you only want those with WEIGHTING relationships, but none which have the PROP property? – InverseFalcon Mar 16 '17 at 17:03

2 Answers2

1

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
Christophe Willemsen
  • 19,399
  • 2
  • 29
  • 36
  • Perfect! Every time I post on SO, I learn about a brand new function in Cypher. `collect` is exactly what I needed - thank you! – Tom Mar 16 '17 at 15:29
1

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
InverseFalcon
  • 29,576
  • 4
  • 38
  • 51