Index not working with $project

I think that this is really the key of the issue. When you do

You are querying the array serviceableMarkets, while with

You are querying the indexed field serviceableMarkets.providerCode.

While the server could try to analyze the inside of $elemMatch to see if all the queried fields are indexed, I think that it might be too complex or expensive to do just to accommodate some edge cases like yours.

I think that once you know $elemMatch might force a fetch before deciding if documents match or not, you can decide not to use it when querying a single indexed array field.

1 Like