https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/
If there are indexes for that field, will the $elemMatch operator use it during $projection?
CodePudding user response:
No, a $project stage will not make use of an index.
Indexes are used for locating documents that match a query without needing to read the entire collection.
A $project stage is used to mutate documents already in the pipeline, so it will never retrieve documents from the collection.
If the pipeline starts with a $project stage, the query executor will add an implicit collection scan (effectively a {$match: {}}
) before the projection.