Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

Neo4j Cypher: Filtering a query results based on another query

I am trying to find a list of names that unique to one query and exclude the ones that are common between the results of two queries. For example, I want the name of the classes that have been taken by students A, B, and C. And exclude from this list the classes that were taken by students D and E. With the help of the answer to this question (Neo4j Cypher: exclude certain nodes from result), I tried this Cypher code and it works, but I get the results as nodes. I want it as a list of names, not nodes.

Match (m:class)-[r]-(n:student) where n.name in ['aa','bb','cc']    
WITH COLLECT(m) AS EXCLUDED
MATCH  (m1:class)-[r1]-(n1:student) where n1.name in ['dd','ee'] 
WITH EXCLUDED, COLLECT(m1) AS included
RETURN FILTER(m1 IN included WHERE NOT m1 IN EXCLUDED)

Thank you!

question from:https://stackoverflow.com/questions/65906762/neo4j-cypher-filtering-a-query-results-based-on-another-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can have another WITH and UNWIND.

Match (m:class)-[r]-(n:student) where n.name in ['aa','bb','cc']    
WITH COLLECT(m) AS EXCLUDED
MATCH  (m1:class)-[r1]-(n1:student) where n1.name in ['dd','ee'] 
WITH EXCLUDED, COLLECT(m1) AS included
WITH FILTER(m1 IN included WHERE NOT m1 IN EXCLUDED) as _results
UNWIND _results as results
RETURN results.name

Note: FILTER() is deprecated in neo4j 3.5 and removed in 4.0 in favor of List Comprehension


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...