Hi there!
I have an index that looks like this:
{ a: 1, 'array.b': 1, 'array.0.date': -1 }
Then there is a query for which this index was specifically tailored:
db.myCollection.find({
"array.b" : ObjectId(...),
"a" : ObjectId(...)
}, { ... bunch of fields }).sort({ "array.0.date" : -1 }).skip(0).limit(26);
Mongo 3.4 picks the right index and uses it to get exact 26 records to work with, resulting that explain shows 26 works in total on step 1.
Later versions (so far tested 3.6 and 4.0) for some reason never use the index to sort records and query ends up traversing every document matched by first two index fields and then sorting everything in memory. Even when Mongo considers using the mentioned index (either by hint or examining execution stats for a specific plan) it still does the same.
I’m trying to figure out a possible upgrade path without performance degrading heavily due to such index misuse. Any tips?
Thanks!