apoc.map.groupByMulti

Details

Syntax

apoc.map.groupByMulti(values, key)

Description

Creates a MAP of the LIST<ANY> values keyed by the given property, with the LIST<ANY> values.

Arguments

Name

Type

Description

values

LIST<ANY>

A list of map values to be grouped.

key

STRING

The key to group the map values by.

Returns

MAP

Usage examples

The following creates a map keyed by club, with list values:

apoc.map.groupByMulti
WITH [
	{name: "Cristiano Ronaldo", club: "Juventus"},
    {name: "Lionel Messi", club: "Barcelona"},
    {name: "Aaron Ramsey", club: "Juventus"},
    {name: "Luiz Suarez", club: "Barcelona"}
] AS listOfMaps, "club" AS grouping
RETURN apoc.map.groupByMulti(listOfMaps, grouping) AS output;
Cypher’s Map Comprehension
WITH [
	{name: "Cristiano Ronaldo", club: "Juventus"},
    {name: "Lionel Messi", club: "Barcelona"},
    {name: "Aaron Ramsey", club: "Juventus"},
    {name: "Luiz Suarez", club: "Barcelona"}
] AS listOfMaps, "club" AS grouping
RETURN { g IN COLLECT {
    UNWIND listOfMaps AS m
    WITH m[grouping] AS group, collect(m) AS members
    RETURN [group, members]
  } | g[0]: g[1] } AS output
Results
output
{
    "Juventus": [
      {
        "name": "Cristiano Ronaldo",
        "club": "Juventus"
      },
      {
        "name": "Aaron Ramsey",
        "club": "Juventus"
      }
    ],
    "Barcelona": [
      {
        "name": "Lionel Messi",
        "club": "Barcelona"
      },
      {
        "name": "Luiz Suarez",
        "club": "Barcelona"
      }
    ]
  }