I want to split a list of nodes in seperate lists

Hi,

I have a list of nodes of different types (labels).
I now want to split that list into seperate ones for each type.

Eg:

(:Person), (:Person), (:Supplier), (:Person), (:Customer)

as result I want to establish three lists, one for Person, Supplier and Customer.

The Person list should contain 3 entries, the Supplier and Customer list have each one entry.

How can I establish this? Apoc would also be fine....

Thanks in advance!

Something like this:

MATCH (n)
RETURN collect([labels(n), properties(n)]) AS labelPropertyMap

(then you can separate the labels, filter them, and scan the map for each [ label, properties ] you want).

1 Like

Does this work?

Test Data:

create (:Person {id: 0}),(:Person {id: 1}),(:Person {id: 2}),(:Supplier{id:3}),(:Customer{id:4})

The solution assumes each node has a single label.

match(n:Person|Customer|Supplier)
return head(labels(n)), collect(n)

1 Like

Hi,

thanks man. At the end it was simple, but I didn't come up with an idea.

For my case, I have to access the different types of nodes in parts of the main query, so I have to store them in a map... This was the complete solution:

match(n:Person|Supplier|Customer)
with head(labels(n)) as key, collect(n) as value
with collect(key) as keys, collect(value) as values
with apoc.map.fromLists(keys,values) as groupedItems

// Do something with the person nodes...
return groupedItems["Person"]

Thanks again for the help!

1 Like