With the query:
[ {$project: {'time': 0}}, {$match: {'time': {$lte: new Date('2019-02-13T11:36:03.481Z')}}}, ]
The optimized plan looks like:
"stages" : [ { "$cursor" : { "queryPlanner" : { "namespace" : "test.system.buckets.ts", "indexFilterSet" : false, "parsedQuery" : { "$and" : [ { "_id" : { "$lte" : ObjectId("5c640124ffffffffffffffff") } }, { "control.max.time" : { "$_internalExprLte" : ISODate("2019-02-13T12:36:03.481Z") } }, { "control.min.time" : { "$_internalExprLte" : ISODate("2019-02-13T11:36:03.481Z") } } ] }, "queryHash" : "A79A3A87", "planCacheKey" : "A79A3A87", "maxIndexedOrSolutionsReached" : false, "maxIndexedAndSolutionsReached" : false, "maxScansToExplodeReached" : false, "winningPlan" : { "stage" : "CLUSTERED_IXSCAN", "filter" : { "$and" : [ { "_id" : { "$lte" : ObjectId("5c640124ffffffffffffffff") } }, { "control.max.time" : { "$_internalExprLte" : ISODate("2019-02-13T12:36:03.481Z") } }, { "control.min.time" : { "$_internalExprLte" : ISODate("2019-02-13T11:36:03.481Z") } } ] }, "direction" : "forward", "minRecord" : ObjectId("000000000000000000000000"), "maxRecord" : ObjectId("5c640124ffffffffffffffff") }, "rejectedPlans" : [ ] } } }, { "$_internalUnpackBucket" : { "exclude" : [ "time" ], "timeField" : "time", "metaField" : "tag", "bucketMaxSpanSeconds" : 3600, "assumeNoMixedSchemaData" : true, "wholeBucketFilter" : { "control.max.time" : { "$lte" : ISODate("2019-02-13T11:36:03.481Z") } }, "eventFilter" : { "time" : { "$lte" : ISODate("2019-02-13T11:36:03.481Z") } } } } ]
It appears as if the $match is getting pushed before the $project. The $cursor stage fetches the matching documents, and then the $_internalUnpackBucket excludes the time field, which represents the $project.
For this query to be correct, a $match should not be pushed before a $project if the $project modifies a field included in the $match.
- is caused by
-
SERVER-70269 Avoid applying match filter to the unpacked document when the whole bucket matches
- Closed