8SINS
8SINS

Reputation: 461

How to sort result by order in mongodb aggregate pipeline

I have an aggregate pipeline that is supposed to display an array of inventory activities by month, from the former to the latter part of each month.

exports.InventoryTable = asyncHandler(async (req, res, next) => {
    Log.aggregate([
    
        {
          $group: {
            _id: {
                name: '$name',
                quantity: '$quantity',
                year: {$year: '$updatedAt'},
                month: {$month: '$updatedAt'},
                dayOfMonth: {$dayOfMonth: '$updatedAt'}
            },
            
            totalAmountSold: { $sum :'$modified_quantity' },
          },
        },
     ]).exec((err, results) => {
        if (err) throw err;
        res.json(results);
    });                 
      
    });

and here is the sample output :


[
    {
        "_id": {
            "name": "Pop",
            "quantity": 58,
            "year": 2020,
            "month": 6,
            "dayOfMonth": 21
        },
        "totalAmountSold": -57
    },
    {
        "_id": {
            "name": "Cement",
            "quantity": 51,
            "year": 2020,
            "month": 6,
            "dayOfMonth": 21
        },
        "totalAmountSold": -50
    },
    {
        "_id": {
            "name": "Washing Machine",
            "quantity": 85,
            "year": 2020,
            "month": 6,
            "dayOfMonth": 21
        },
        "totalAmountSold": -20
    },
    {
        "_id": {
            "name": "Pop",
            "quantity": 4,
            "year": 2020,
            "month": 6,
            "dayOfMonth": 14
        },
        "totalAmountSold": -15
    },

    
    {
        "_id": {
            "name": "Cement",
            "quantity": 1,
            "year": 2020,
            "month": 6,
            "dayOfMonth": 20
        },
        "totalAmountSold": -17
    },
    {
        "_id": {
            "name": "Pop",
            "quantity": 24,
            "year": 2020,
            "month": 6,
            "dayOfMonth": 8
        },
        "totalAmountSold": -6
    }
]

I would want to have the result displayed in the reverse order with the earliest record at the top of the document. I have tried making use of the sort() function :

 ]).sort({"id":-1}).exec((err, results) => {
        if (err) throw err;
        res.json(results);
    });                 

````
but the record still stays the same. on second thought, I might be applying the "sort" incorrectly. I really require help with this one.

Upvotes: 2

Views: 236

Answers (1)

mickl
mickl

Reputation: 49945

You need to use $sort within the pipeline:

{
    $group: {
        _id: {
            name: '$name',
            quantity: '$quantity',
            year: {$year: '$updatedAt'},
            month: {$month: '$updatedAt'},
            dayOfMonth: {$dayOfMonth: '$updatedAt'}
        },    
        totalAmountSold: { $sum :'$modified_quantity' },
    }
},
{
    $sort: {
        "_id.year": 1,
        "_id.month": 1,
        "_id.dayOfMonth": 1,
    }
}

Upvotes: 3

Related Questions