Issue Description
Date announced: March 17, 2026
We have identified a critical issue affecting the following MongoDB versions:
8.0.0-8.0.19
8.2.0-8.2.3
Specific aggregation queries that contain $sort and $group stages and $top/$bottom accumulators can return incorrect results.
You are not impacted by this issue if:
You have not run an affected version of MongoDB: 8.0.0-8.0.19 or 8.2.0-8.2.3;
Your queries do not contain
$top/$bottomin a$groupstage immediately after a$sortstage; orYour aggregation query includes earlier stages that prevent index-based distinct scan optimization (for example:
$unwind,$group,$replaceRoot), or your$sortstage cannot be satisfied by an index.
MongoDB versions 8.0.20 and 8.2.4 already contain fixes for this issue.
Details
This issue can only manifest if:
MongoDB version is 8.0.0-8.0.19 or 8.2.0-8.2.3 (versions 8.2.x are only affected if the
featureFlagShardFilteringDistinctScanis changed from the default value of 'true' to 'false'); andYour application runs an aggregate query where ALL of the following are true:
A
$sortstage is immediately followed by a$groupstageThe
$groupstage contains$topor$bottomaccumulatorsThe query uses a compound index that satisfies the pattern in the
$sortstageThe group key ('_id' in
$group) is a single field and is the prefix of the indexThe accumulator's
sortByis referencing a field that immediately follows the leading index field, and does not match the sort direction in$sort
If your application queries meet the criteria above, we recommend that you upgrade to the latest MongoDB patch version.
Fixes and Remediation
For MongoDB Atlas Customers: Your Atlas clusters have already been upgraded to versions containing fixes for these issues, unless you have configured a maintenance window. If you have configured a maintenance window, the change will be applied during the next scheduled window, or you may choose to apply the change immediately using the Begin Maintenance Now button.
For Enterprise Advanced and Self-Managed Customers: MongoDB patch versions 8.0.20 and 8.2.4 already contain fixes for these issues.
For more information, see SERVER-110803. If you have any questions, open a support case or start a chat with the Atlas Support team.