Pipeline and index: performance doubt

Hi to all,

I have a big doubt:
My pipeline begins with a match operator and continues with three unwind operators, there is another match, then a sort and limit and terminates with a projection.
Can I have a performance gain if I add an index on the key used to sort inside the pipeline? An if I add an index on the key used by the second match?

Thank you all,
I appreciate any comments or suggestions.

I am afraid that after the unwind no index can be used for the subsequent match and sort. The best would be to find a way to move the all the matches and sorts at the top of the pipeline. But it is not always possible.

1 Like

Welcome to the community, @Alaskent19 !

Indeed, as @steevej already have mentioned, after $unwind stage you can’t use indexes.

But, if you’re flexible on modifying the aggregation or document schema, you can share:

  • prettified sample document from the collection
  • prettified desired output of your aggregation
  • the basic description of what you’re trying to achieve with your aggregation and possible nuances
2 Likes