richardabced
richardabced

Reputation: 323

Neo4j return Node which relationships link to a certain Label

Is it possible to return nodes which do not relate to a specific label at the end of the relationship links. E.g.

(EndTargetNode)<--MYREL---(SomeNode)<--MYREL---(SomeNode)<--MYREL---(SomeNode)
                                               /   |
                                              /  MYREL
(Node)<---SOME_REL---(SomeNode)<----MYREL----      |
                                                   v
                                               (SomeNode)
                                                   |
                                                 MYREL
                                                   |
                                                   v
                                               (SomeNode)

I'd like to match all SomeNodes which do not end with a MYREL relation to a Node with type EndTargetNode AND have at least 2 MYREL relationships. How would I go about this? Thanks

So far i've manage to just find nodes with the multiple MYREL relationships:

match (n)-[rel:MYREL]->(e) 
with n, count(e) as rels
where rels > 1
return n;

Upvotes: 1

Views: 75

Answers (1)

Frank Pavageau
Frank Pavageau

Reputation: 11735

This query will do it and reads just like your definition:

// I want all :SomeNode
MATCH (n:SomeNode)
// not related to an :EndTargetNode
WHERE NOT (n)-[:MYREL]->(:EndTargetNode)
// and with at least 2 MYREL relationships
AND size((n)-[:MYREL]->()) >= 2
RETURN n

Upvotes: 2

Related Questions