Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-9755

Aggregate options order (sort before group do not work)

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Won't Do
    • Icon: Major - P3 Major - P3
    • Server_Docs_20231030
    • None
    • manual
    • None

    Description

      I really don't know if its a bug (because the documentation its no clear about the behavior). However it is not 'intuitive' and it takes a long time to realize where is the problem.

      note: (if we add 'sort' before 'group') aggregate do not return values ordered by total

      WRONG BEHAVIOR:

          db.connection.collection( statsCollection ).aggregate( [
                  { $match : { } },
                  { $unwind: '$states' },
                  { $sort : { total : -1 } },
                  { $group : { _id: "$states._id" , total: { $sum: "$states.count" } } }
              ],
              function(err, result) {
                  if(err) 
                      res.status(404).json({ status : 'error' })
                  else{
                      console.log(result);    
                      res.send(result)
                  };
              }
          );
      

      GOOD BEHAVIOR:

          db.connection.collection( statsCollection ).aggregate( [
                  { $match : { } },
                  { $unwind: '$states' },
                  { $group : { _id: "$states._id" , total: { $sum: "$states.count" } } },
                  { $sort : { total : -1 } }
              ],
              function(err, result) {
                  if(err) 
                      res.status(404).json({ status : 'error' })
                  else{
                      console.log(result);    
                      res.send(result)
                  };
              }
          );
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            karelbecerra Karel Becerra
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              1 year, 14 weeks, 2 days ago