Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j how to compare two list and return the different items

Tags:

neo4j

I am using neo4j MATCH and get two list of User: listA,listB, and listB is part of listA
how can I return users only in listA but not in listB use cipher query
the cipher like :

MATCH listA, listB
RETURN listA - listB

Here is my previous question: neo4j cypher multi relationship between nodes

Done See solution in the link above

like image 481
Awakening Avatar asked Jan 12 '23 14:01

Awakening


2 Answers

To return members of one list that are not in another you can use the FILTER function (docs), for example

WITH [1,2,3,4,5,6] as listA, [1,2,3] as listB
RETURN FILTER( n IN listA WHERE NOT n IN listB ) as listC

c
4, 5, 6
Returned 1 row in 90 ms
like image 110
jjaderberg Avatar answered Feb 23 '23 23:02

jjaderberg


Filter function is removed in the latest Neo4j version 4.0 doc here instead, use List comprehension

WITH [1,2,3,4,5,6] as listA, [1,2,3] as listB
RETURN [n IN listA WHERE NOT n IN listB] as listC

This results in an output 4, 5, 6

like image 21
Naveen Avatar answered Feb 23 '23 23:02

Naveen