mbouclas
mbouclas

Reputation: 744

Match where AND on the same relationship

Given the following schema, the node in the middle is called Business and the sattelite ones are called BusinessType. The relationship is one to many as one Business has many BusinessType nodes. What I'm trying to do is get all Business nodes where the BusinessType equals a set of codes. The problem is that I'm getting back Business nodes which only have one part of the criteria. For example:

MATCH (business:Business {uuid: 'f872e7d4-1105-11ea-9461-4cedfb791f65'})-[r:BUSINESS_HAS_TYPE]->(bt:BusinessType) 
return *

returns the result bellow.

enter image description here

MATCH (business:Business) 
MATCH (business)-[r:BUSINESS_HAS_TYPE]->(bt:BusinessType) 
WHERE bt.slug = 'rail-doors' AND bt.slug = 'pergolas'
RETURN business

What i'm after in the query above is to get all Business nodes where their BusinessType matches 'rail-doors' AND 'pergolas'. This returns null for some reason though clearly there's a potential match as shown from the previous query.

I also tried using ['rail-doors','pergolas'] but that's clearly wrong cause it returns any Business that has either one of the two.

Any input is welcome

Upvotes: 1

Views: 36

Answers (1)

cybersam
cybersam

Reputation: 66999

A single node cannot have 2 different values for the same property at the same time. You want a query that looks for each desired value across all related :BusinessType nodes.

Try this query, instead:

MATCH (business:Business)-[:BUSINESS_HAS_TYPE]->(bt:BusinessType)
WITH business, COLLECT(bt.slug) AS slugs
WHERE ALL(x IN ['rail-doors', 'pergolas'] WHERE x IN slugs)
RETURN business 

Upvotes: 1

Related Questions