Currently clauses in a $all will be evaluated in the order that they are provided to the query. This can lead to cases where a superset is being searched to find values that are contained within a smaller subset of the wanted values.
Consider trying to find all documents that have tags showing that they are an Italian and have a Michelin star within a collection of restaurants. There will be a large number of documents with the tag "Italian" but very few with the tag "Michelin".
If the tags are entered into the $all clause in alphabetical order, the search will take longer as the larger set of "Italian" documents must be searched again for the "Michelin" tag. If the tag order is reversed, then we search the smaller set of "Michelin" matching documents to find those who match "Italian".
The optimisation here would be to remove the dependency on the order of documents in an $all clause.
Changes in this case should handle situations where the entities within the $all clause are not simply fields, but also documents which may have several levels of depth.