Reputation: 3102
If I have a large collection (40m+ objects) and I'm looking to find:
(a : 1 OR b : 2) && c : 3
sorted by d : 1
I understand that I would create an index:
{ "a":1, "b":1, "c":1, "d":1 }
But if I wanted to allow the sort order to be reversed, would I need an additional index:
{ "a":1, "b":1, "c":1, "d":-1 }
Many thanks.
Upvotes: 0
Views: 608
Reputation: 3618
If you want the result sorted in increasing or decreasing order, compound indexes may fail you. Remember that compound indexes sort the tuples in lexicographical order. There is no reason to believe that the tuples are always in increasing order on the last key (d).
You could simply do the following:
db.foo.find(...).sort({d:1});
db.foo.find(...).sort({d:-1});
Sorting, even on large sets of tuples is fairly fast.
Upvotes: 2