Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compound Indexes in Mongo and sorting

I have this index a,b,-c on a Mongo collection.

When I run this query: find by a,b,f,(sort by) -c, will the index on c make sorting fast?

like image 663
Jimmy Avatar asked Mar 30 '11 19:03

Jimmy


1 Answers

Yes, the compound index on those fields will make the following queries fast:

  1. find(a)
  2. find(a,b)
  3. find(a,b,c)

As long as the sort order is the same sort order as in the index, that is. If you reverse the sort order on any of those three keys, Mongo will do a full collection scan rather than using than using the index.

like image 169
Srdjan Pejic Avatar answered Nov 01 '22 02:11

Srdjan Pejic