[DOCS-9755] Aggregate options order (sort before group do not work) Created: 10/Jan/17  Updated: 30/Oct/23

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Improvement Priority: Major - P3
Reporter: Karel Becerra Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 1 year, 14 weeks, 2 days ago
Epic Link: DOCSP-1769

 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)
            };
        }
    );



 Comments   
Comment by Education Bot [ 31/Oct/22 ]

Hello! This ticket has been closed due to inactivity. If you believe this ticket is still important, please reopen it and leave a comment to explain why. Thank you!

Comment by Karel Becerra [ 10/Jan/17 ]

Thanks Thomas!

Just to put things in a better (clear) way. The issue is related with "how aggregate function evaluates the parameters".

It looks like if $sort it is evaluated before $group then the variables 'do not exists' and therefore the $sort do not work.

Comment by Kelsey Schubert [ 10/Jan/17 ]

Hi karelbecerra,

Thanks for the report. This is expected behavior and is documented here: https://docs.mongodb.com/manual/reference/operator/aggregation/group/#pipe._S_group.

Please note that the TOOLS project is for reporting bug or feature suggestions for the tools that ship with MongoDB: mongorestore, mongotop, etc. Therefore, I've moved this to the DOCS project to track improvements to our documentation, which would help clarify this behavior.

Kind regards,
Thomas

Generated at Thu Feb 08 07:59:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.