Search code examples
mongodbaggregatepipelinegroup

MongoDB pipeline group and take all data from one column and make the average


enter image description herei have a dataset with some columns like countries, players, kd_ratio

i want to write a pipeline where i can group kd_ratio from all the countries and get the average.

Im trying this:

db.CSGO.aggregate([
{
    $group: {
        _id: { country: { $country: "$all" }, nick: { $nick: "$all" } },
        averageQuantity: { $avg: "$kd_ratio" },
        
    }}])

Solution

  • Based on what rickhg12hs the code will be this:

    db.CSGO.aggregate([
        {
            $group: {
                _id:  '$country' ,
                averageQuantity: { $avg: "$kd_ratio" },
            
            }}])