Search code examples
neo4jcypherapoc

Neo4j Cypher alternative for apoc.coll.union and apoc.coll.unionAll


In order to substitute

apoc.coll.unionAll(list1, list2) as list3 

looks like I may use + sign in pure Cypher:

WITH [1,2] as list1, [1,2,3,4] as list2 WITH list1 + list2 as list3 

but how to implement apoc.coll.union in plain Cypher which returns a distinct union of the 2 lists?


Solution

  • You can unwind the combined list then collect the distinct elements as union list.

    WITH [1,2] as list1, [1,2,3,4] as list2 
    UNWIND list1 + list2 as l 
    RETURN collect(distinct l) as union_list
    

    Result:

    ╒════════════╕
    │"union_list"│
    ╞════════════╡
    │[1,2,3,4]   │
    └────────────┘