Skip to main content

MongoDB System Alert: Potential incorrect results from certain aggregations with $top/$bottom (SERVER-110803)

Certain aggregation queries that include $sort and $group stages, along with $top or $bottom accumulators, may return incorrect or unexpected results.

Written by Diogo
Updated today

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/$bottom in a $group stage immediately after a $sort stage; or

  • Your aggregation query includes earlier stages that prevent index-based distinct scan optimization (for example: $unwind, $group, $replaceRoot), or your $sort stage 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 featureFlagShardFilteringDistinctScan is changed from the default value of 'true' to 'false'); and

  • Your application runs an aggregate query where ALL of the following are true:

    • A $sort stage is immediately followed by a $group stage

    • The $group stage contains $top or $bottom accumulators

    • The query uses a compound index that satisfies the pattern in the $sort stage

    • The group key ('_id' in $group) is a single field and is the prefix of the index

    • The accumulator's sortBy is 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.

Did this answer your question?