Pipeline Optimization: What counts as data transformation?

In the lecture on aggregation performance, the instructor mentions that once you transform the data then you can no longer take advantage of indexing in future stages. I’m a little confused on what actually counts as data transformation and what stages you should avoid before you have finished with operations that take advantage of indexing. There’s the obvious stages that can fundamentally change the shape of the data like $group and $unwind. When the instructor says indexes can’t be used after any data transformation, does that include stages like $addFields or $project? Does it depend on what is added?

Thanks for your help.

1 Like


The aggregation pipeline will only use indexes for the $match and $sort stages; see the documentation here for details. Notice however, in the documentation here, that the pipeline optimizer may in some case move stages to get better performance. Hope that helps. Good luck.

1 Like