0

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?

  • I agree with [@Joe's answer](https://stackoverflow.com/a/74905673/20042973). But I'm a little curious about the specifics of your question. In what way(s) were you thinking that the index may be used there? – user20042973 Dec 24 '22 at 14:13

1 Answers1

2

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.

Joe
  • 25,000
  • 3
  • 22
  • 44