如何在单个语句中减去 Neo4j 中具有不同条件的相同返回值
How can I subtract the same returned values with different conditions in Neo4j in a single statement
我正在使用 Java 来匹配我这样的陈述。
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");
那么我如何在一次匹配中减去这两个语句而不需要创建另一个结果 (secondResult)?
如果您将每个都放在一个集合中,然后过滤掉一个集合中不在另一个集合中的那些,您将有效地从另一个中减去一个。
// 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 )
我正在使用 Java 来匹配我这样的陈述。
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");
那么我如何在一次匹配中减去这两个语句而不需要创建另一个结果 (secondResult)?
如果您将每个都放在一个集合中,然后过滤掉一个集合中不在另一个集合中的那些,您将有效地从另一个中减去一个。
// 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 )