'Mongodb is not using best index for query always, rather using index which make execution time more

Query is run on five fields for more than 100000 times with different values of those fields every time.

For some queries, mongoDB is using best index to filter data while sometime it is not using best index rather most of the times, it is using index which is not efficient.

I confirmed above by checking execution stats of those queries and plan summary.

I checked and confirmed that by running manual query on server by providing hint for index for different values.

Now, query should uses different index for different values of fields , thats why i can not hard code any particular index in hint.

Any Solution for this ??



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source