1

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 } ]
2

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.