Skip to main content

Advanced Aggregation Pipelines

You can also use aggregation pipelines to perform more advanced operations on your data.

Exercises​

Try finding the answer to the following questions using aggregation pipelines.

What is the name of the player with the highest power_stamina?​

Answer
[
{
$sort:
/**
* Provide any number of field/order pairs.
*/
{
power_stamina: -1,
},
},
{
$limit:
/**
* Provide the number of documents to limit.
*/
1,
},
{
$project:
{long_name: 1},
{_id: 0}
}
]

What is the least common club_position?​

Answer
[
{
$group:
/**
* _id: The id of the group.
* fieldN: The first field name.
*/
{
_id: "$club_position",
count: {
$sum: 1,
},
},
},
{
$sort:
/**
* Provide any number of field/order pairs.
*/
{
count: 1,
},
},
{
$limit:
/**
* Provide the number of documents to limit.
*/
1,
},
]

Find the top three teams with the highest value of player value_eur.​

Answer
[
{
$group:
/**
* _id: The id of the group.
* fieldN: The first field name.
*/
{
_id: "$club_name",
total_value: {
$sum: "$value_eur",
},
},
},
{
$sort:
/**
* Provide any number of field/order pairs.
*/
{
total_value: -1,
},
},
{
$limit:
/**
* Provide the number of documents to limit.
*/
3,
},
];