As can be seen in the repro script, the special timeseries field time is actually renamed replaced by the $addFields stage by computing the value referencing another special field tag's any subfield and then the next $project stage excludes it but the timeseries optimization does not exclude it as follows:
> db.ts.explain().aggregate([{$addFields: {time: {$dateFromParts: {year: "$tag.none"}}}}, {$project: {tag: 1}}]); { "explainVersion" : "1", "stages" : [ { "$cursor" : { "queryPlanner" : { "namespace" : "test.system.buckets.ts", "indexFilterSet" : false, "parsedQuery" : { }, "queryHash" : "FCBE9F38", "planCacheKey" : "64E90EFC", "optimizationTimeMillis" : 1, "maxIndexedOrSolutionsReached" : false, "maxIndexedAndSolutionsReached" : false, "maxScansToExplodeReached" : false, "prunedSimilarIndexes" : false, "winningPlan" : { "isCached" : false, "stage" : "COLLSCAN", "direction" : "forward" }, "rejectedPlans" : [ ] } } }, { "$addFields" : { "time" : { "$dateFromParts" : { "year" : "$meta.none" } } } }, { "$_internalUnpackBucket" : { "include" : [ "_id", "tag" ], "timeField" : "time", "metaField" : "tag", "bucketMaxSpanSeconds" : 3600, "assumeNoMixedSchemaData" : true, "computedMetaProjFields" : [ "time" ] } } ],
It does not seems that dependency tracking or inclusion/exclusion tracking for special timeseries fields work correctly and it has been introduced around 7.3 timeframe, seeing it's failing on v7.3 branch as well.
Ideally, we would want to totally remove $addFields as it's excluded by the subsequent $project while optimizaing the pipeline
The simplest fix would be to not push down $addFields when it actually renames the timeseries special field timeField.
- related to
-
SERVER-87961 Time series $group rewrite may produce incorrect results when a preceding $project stage projects out accessed fields
- Closed