Newby MongoDB & CosmosDB user here, I've read the answer to this question How does MongoDB treat find().sort() queries with respect to single and compound indexes? and the offocial MongoDB docs and I believe my index creation mirrors that answer so I am leaning towards this being a CosmosDB issue but reading their documentation CosmosDB 3.6 supports compounded indexes as well, so I am at a loss right now.
I am able to run sort() queries like db.Videos.find().sort({"PublishedOn": 1})
from the mongo command line on a collection with an index created as db.Videos.createIndex({"PublishedOn": 1})
or db.Videos.createIndex({"PublishedOn": -1})
.
And when I add a 'where' clause to the find like this db.Videos.find({"IsPinned": false}).sort({"PublishedOn": 1})
the above index still works.
However I now have document look ups which I want to avoid, so I drop the above single field index and create a compounded index like this db.Videos.createIndex({"IsPinned": 1, "PublishedOn": 1})
or db.Videos.createIndex({"PublishedOn": 1, "IsPinned": 1})
but now the query always fails with the error The index path corresponding to the specified order-by item is excluded.
.
Is this a limitation of CosmosDB or is my 'ordering' in the index bad?
The index path corresponding to the specified order-by item is excluded
is specific to Cosmos' API. I expect the index on{"PublishedOn": 1, "IsPinned": 1}
should be usable (or rather, would be used by MongoDB based on your query). It seems Cosmos may require different index definitions to support sorting. Perhaps thePublishedOn
sort order in the index has to match the query? – Stennie