ISSUE SUMMARY
Given a compound index on a collection, queries producing empty results over a field fail if there's a subsequent sort stage over the next field in the index. Here are two examples with the $in operator:
db.col.ensureIndex({a : 1, b : 1, c : 1}) db.col.find({a : {$in : []}}).sort({b : 1}) db.col.find({a : 1, b : {$in : []}}).sort({c : 1})
Other queries yielding empty bounds will also trigger the issue. For example, if you have non-multikey index {a: 1, b: 1, c: 1}, the assert will be triggered by:
db.col.find({a : {$gt : 5, $lt : 4}}).sort({b : 1}) db.col.find({a : 1, b : {$gt : 5, $lt : 4}}).sort({c : 1})
USER IMPACT
Queries of this type fail with an assertion error. Queries of this type should be extremely rare, as the result is always empty.
WORKAROUNDS
There are no known workarounds.
AFFECTED VERSIONS
MongoDB production releases 2.6.0 and 2.6.1 are affected by this issue.
FIX VERSION
The fix is included in the 2.6.2 production release.
RESOLUTION DETAILS
Do not attempt to explode for sort with empty bounds.
Original description
> db.aaa.drop() true test@local(2.6.1-rc0) > db.aaa.ensureIndex({_a:1,_id:1}) { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } socialite@(2.6.1-rc0) > db.aaa.find({ _a: { $in: [ ] } }).sort({ _id: -1 }) error: { "$err" : "assertion src/mongo/db/query/planner_analysis.cpp:114" } >
In the logs
2014-04-26T13:09:59.828-0400 [conn26] socialite.aaa Assertion failure firstOil.intervals.size() >= 1 src/mongo/db/query/planner_analysis.cpp 114 2014-04-26T13:09:59.832-0400 [conn26] socialite.aaa 0x1006ac88b 0x1006643a2 0x100654e32 0x1003ef9a7 0x1003f0b2e 0x1003f1382 0x1003fb8d3 0x1003b4237 0x1003b608e 0x1003b6cbe 0x1003d0987 0x1002a14a0 0x100006ca4 0x100671c51 0x1006e0c95 0x7fff8d42d899 0x7fff8d42d72a 0x7fff8d431fc9 0 mongod 0x00000001006ac88b _ZN5mongo15printStackTraceERSo + 43 1 mongod 0x00000001006643a2 _ZN5mongo10logContextEPKc + 114 2 mongod 0x0000000100654e32 _ZN5mongo12verifyFailedEPKcS1_j + 274 3 mongod 0x00000001003ef9a7 _ZN5mongo20QueryPlannerAnalysis14explodeForSortERKNS_14CanonicalQueryERKNS_18QueryPlannerParamsEPPNS_17QuerySolutionNodeE + 1963 4 mongod 0x00000001003f0b2e _ZN5mongo20QueryPlannerAnalysis11analyzeSortERKNS_14CanonicalQueryERKNS_18QueryPlannerParamsEPNS_17QuerySolutionNodeEPb + 808 5 mongod 0x00000001003f1382 _ZN5mongo20QueryPlannerAnalysis17analyzeDataAccessERKNS_14CanonicalQueryERKNS_18QueryPlannerParamsEPNS_17QuerySolutionNodeE + 686 6 mongod 0x00000001003fb8d3 _ZN5mongo12QueryPlanner4planERKNS_14CanonicalQueryERKNS_18QueryPlannerParamsEPSt6vectorIPNS_13QuerySolutionESaIS9_EE + 11241 7 mongod 0x00000001003b4237 _ZN5mongo19getRunnerAlwaysPlanEPNS_10CollectionEPNS_14CanonicalQueryERKNS_18QueryPlannerParamsEPPNS_6RunnerE + 119 8 mongod 0x00000001003b608e _ZN5mongo9getRunnerEPNS_10CollectionEPNS_14CanonicalQueryEPPNS_6RunnerEm + 1790 9 mongod 0x00000001003b6cbe _ZN5mongo9getRunnerEPNS_14CanonicalQueryEPPNS_6RunnerEm + 158 10 mongod 0x00000001003d0987 _ZN5mongo11newRunQueryERNS_7MessageERNS_12QueryMessageERNS_5CurOpES1_ + 3847 11 mongod 0x00000001002a14a0 _ZN5mongo16assembleResponseERNS_7MessageERNS_10DbResponseERKNS_11HostAndPortE + 1968 12 mongod 0x0000000100006ca4 _ZN5mongo16MyMessageHandler7processERNS_7MessageEPNS_21AbstractMessagingPortEPNS_9LastErrorE + 308 13 mongod 0x0000000100671c51 _ZN5mongo17PortMessageServer17handleIncomingMsgEPv + 1681 14 mongod 0x00000001006e0c95 thread_proxy + 229 15 libsystem_pthread.dylib 0x00007fff8d42d899 _pthread_body + 138 16 libsystem_pthread.dylib 0x00007fff8d42d72a _pthread_struct_init + 0 17 libsystem_pthread.dylib 0x00007fff8d431fc9 thread_start + 13 2014-04-26T13:09:59.833-0400 [conn26] assertion 0 assertion src/mongo/db/query/planner_analysis.cpp:114 ns:socialite.aaa query:{ $query: { _a: { $in: [] } }, $orderby: { _id: -1 } } 2014-04-26T13:09:59.833-0400 [conn26] ntoskip:0 ntoreturn:-50 2014-04-26T13:09:59.833-0400 [conn26] query socialite.aaa query: { $query: { _a: { $in: [] } }, $orderby: { _id: -1 } } keyUpdates:0 exception: assertion src/mongo/db/query/planner_analysis.cpp:114 numYields:0 locks(micros) r:5341 reslen:89 5ms