KJansma
KJansma

Reputation: 35

Neo4j AND clause

New to Neo4j and looking for AND clause in a Cypher query. Suppose I have Person nodes and Role nodes. A person can have multiple Roles (Manager, Leader, IC, QA,...) I want to find the set of People who have both the roles of Manager and QA.

Somethng like: MATCH (p:Person)-[:HAS_ROLE]->(r:Role{name:"Manager"}) AND (p)-[:HAS_ROLE]->(r:Role{name:"QA"}) RETURN p

Upvotes: 0

Views: 187

Answers (2)

TheTeacher
TheTeacher

Reputation: 510

MATCH (p:Person)-[:HAS_ROLE]->(:Role{name:"Manager"})
WHERE (p)-[:HAS_ROLE]->(:Role{name:"QA"})
RETURN p

this above query can be used if its just two Roles.But if want try for Many,then

MATCH (p:Person)-[:HAS_ROLE]->(r:Role)
WITH p, COLLECT(r.name) as roles
WHERE ALL (x IN ["Manager","QA"] WHERE x IN roles)
RETURN p

you can update this ["Manager","QA"] with all the roles you want to check for a person.

Upvotes: 1

Graphileon
Graphileon

Reputation: 5385

You use r twice

Try :Role instead of r:Role

Upvotes: 0

Related Questions