Operational Defect Database

BugZero found this defect 2403 days ago.

MongoDB | 408540

[SERVER-30338] $sort + $limit optimizer failing in some cases

Last update date:


Affected products:

MongoDB Server

Affected releases:


Fixed releases:

No fixed releases provided.



We have discovered an issue that we think related to the $sort + $limit optimizer. We have attached dump of a sample collection. Second query with a limit of 25 has as first element the last element of the first page (25th) instead of the 26th correct one. Same queries performed without limit works properly.

Top User Comments

thomas.schubert commented on Thu, 21 Sep 2017 06:04:17 +0000: Hi ivan.fioravanti@4ward.it, This is likely just the result of your particular system. For example, when I ran the same operations as you described in the ticket, I saw a different sort order than the one you are describing. To ensure a consistent ordering, please include a unique field in your sort order. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-users group. Kind regards, Kelsey ivan.fioravanti@4ward.it commented on Thu, 27 Jul 2017 09:17:55 +0000: But why without $limits it works properly an moreover with $limits results is always the same? It this is by design I expect to see different sorted elements in result, while here order is always wrong in the same way and always on first element of the second page. thomas.schubert commented on Wed, 26 Jul 2017 16:32:11 +0000: Hi ivan.fioravanti@4ward.it, Thank for the very clear reproduction steps in your report. The behavior you're describing is expected, please review my comment on SERVER-28195 for additional details. Kind regards, Thomas

Additional Resources / Links


BugZero Risk Score

Coming soon



Have you been affected by this bug?


Do you know how much operational outages are costing you?

Understand the cost to your business and how BugZero can help you reduce those costs.


Login to read and write comments.

Have you ever...

had your data corrupted from a