I have the following query
db.getCollection("order_error").find({
"$and":[
{
"type":"orderResult"
},
{
"Origin.SN":{
"$in":[
"5701097",
"5701099"
]
}
},
{
"productLowerCase":{
"$regex":"capp",
"$options":"i"
}
}
]
}).sort({"timestamp.milliseconds" : -1}).skip(1).limit(100).explain("executionStats")
And the index this query is using is
type_1_Origin.SN_1_timestamp.milliseconds_-1
The stages are
IXSCAN (For Origin.SN = 5701099) -> IXSCAN (For Origin.SN = 5701097) -> SORT_MERGE -> FETCH -> SKIP -> LIMIT
I can see that the sorting is also happening with index so that part is optimised. But the issue is the FETCH stage. There the filter is as follows
{
"productLowerCase":{
"$regex":"capp",
"$options":"i"
}
}
This part is not optimised. Even if I create an index like productLowerCase_1 and change the query to use the left anchored regex also that index is not even getting used in FETCH stage. Does MongoDB use only one index in a query? Is there any way I can make it use the productLowerCase_1 index along with type_1_Origin.SN_1_timestamp.milliseconds_-1 index in its queries?