Reputation: 631
How can I sort date by: ascending
and descending
order. I already fetched the values from mongodb, but it gives me scrambled order.
Here is my code to count the distinct values of my date:
router.get('/blooddonationpie', function(req, res) {
Blooddonation.aggregate([{$group: {_id : "$date" , count :{$sum:1}}}],function(err, date) {
res.json({ success: true, date: date });
console.log(date);
});
});
Gives me an output with no order:
[ { _id: '09-09-2019', count: 3 },
{ _id: '08-09-2019', count: 3 },
{ _id: '07-09-2019', count: 2 },
{ _id: '05-09-2019', count: 8 },
{ _id: '06-09-2019', count: 1 },
{ _id: '10-09-2019', count: 1 },
{ _id: '04-09-2019', count: 4 } ]
Upvotes: 1
Views: 719
Reputation: 121
db.collection.find().sort({ property: 1})
for ascending order
db.collection.find().sort({ property: -1})
for descending order
for more information follow the link https://docs.mongodb.com/manual/reference/operator/meta/orderby/
Upvotes: 2