apoc.agg.minItems
Function APOC Core
apoc.agg.minItems(item, value, groupLimit: -1) - returns a map {items:[], value:n} where value
is the minimum value present, and items
are all items with the same value. The number of items can be optionally limited.
Usage Examples
The examples in this section are based on the following sample graph of people and their dates of birth:
CREATE (:Person {name: "Tom Hanks", dateOfBirth: date("1956-07-09")});
CREATE (:Person {name: "Tom Cruise", dateOfBirth: date("1962-07-03")});
CREATE (:Person {name: "Nicole Kidman", dateOfBirth: date("1967-06-20")});
CREATE (:Person {name: "Matt Damon", dateOfBirth: date("1970-10-08")});
CREATE (:Person {name: "Carrie Fisher", dateOfBirth: date("1956-10-21")});
CREATE (:Person {name: "Bryan Cranston", dateOfBirth: date("1956-03-07")});
You can collect the people that have the minimal year of birth by running the following query:
MATCH (p:Person)
WITH apoc.agg.minItems(p, p.dateOfBirth.year) AS minItems
RETURN minItems.value AS value, minItems.items AS items;
value | items |
---|---|
1956 |
[(:Person {name: "Tom Hanks", dateOfBirth: 1956-07-09}), (:Person {name: "Carrie Fisher", dateOfBirth: 1956-10-21}), (:Person {name: "Bryan Cranston", dateOfBirth: 1956-03-07})] |
You can set the groupLimit
(3rd parameter) to set a limit on the number of items returned:
MATCH (p:Person)
WITH apoc.agg.minItems(p, p.dateOfBirth.year, 2) AS minItems
RETURN minItems.value AS value, minItems.items AS items;
value | items |
---|---|
1956 |
[(:Person {name: "Tom Hanks", dateOfBirth: 1956-07-09}), (:Person {name: "Carrie Fisher", dateOfBirth: 1956-10-21})] |