apoc.map.groupBy

This function is deprecated. Use Cypher’s map comprehension instead.

Details

Syntax

apoc.map.groupBy(values, key)

Description

Creates a MAP of the LIST<ANY> keyed by the given property, with single 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 a single value

apoc.map.groupBy
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 group
RETURN apoc.map.groupBy(listOfMaps, groups) 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 group
RETURN { map IN listOfMaps | map[group]: map } AS output;
Results
output
{
  "Juventus": {
    "name": "Aaron Ramsey",
    "club": "Juventus"
  },
  "Barcelona": {
    "name": "Luiz Suarez",
    "club": "Barcelona"
  }
}