AbdelRahman Badr
AbdelRahman Badr

Reputation: 293

How can I subtract the same returned values with different conditions in Neo4j in a single statement

I'm using Java to match my statements like this.

Result firstResult = db.execute("MATCH (n)-[:someRelation]->(m) WHERE n.name='firstN' RETURN m.cal");
Result secondResult = db.execute("MATCH (n)-[:someRelation]->(m) WHERE n.name='Second' RETURN m.cal");

So how can I subtract these two statements in a single match without needing to create another Result (secondResult) ?

Upvotes: 4

Views: 695

Answers (1)

Dave Bennett
Dave Bennett

Reputation: 11216

If you put each in a collection and then filter out the ones in one collection that aren't in the other you will effectively minus one from the other.

// put the result of the first set in a collection
MATCH (n)-[:someRelation]->(m) 
WHERE n.name='firstN' 
WITH collect(m.cal) as m_cal_coll_1
//
// put the second set in a collection
MATCH (n)-[:someRelation]->(m) 
WHERE n.name='Second'
WITH m_cal_coll_1, collect(m.cal) as m_cal_coll_2
//
// return items in the first that are not in the second
return filter(x IN m_cal_coll_1 WHERE not x in m_cal_coll_2 )

Upvotes: 5

Related Questions