-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Optimization
Extracting fields into variables just to use them later in a group stage is unnecessary and should be optimized away.
Query optimizer should move as many entries from the project stage as possible into the group stage provided such entries are only used in such group stage. See the following example:
...aggregate([ { $project: { "a": "$path.to.a", "b": "$path.to.b", ... }}, { $group: { _id: { "a": "$a", "b": "$b", ... } }} ])
results in a group < -- projection_default < -- collscan plan
While the following
...aggregate([ { $group: { _id: { "a": "$path.to.a", "b": "$path.to.b", ... } }} ])
results in a simpler group <- collscan plan
And produces same output