index on some field sort descending/ascending issue #3

shoujs opened this Issue Aug 23, 2012 · 2 comments


None yet

2 participants

shoujs commented Aug 23, 2012

I have a collection, "features", with 3 fields, name, active, weight,

I query the collection by weight descending:

db.features.find({active:true},{name:1, weight:1}).sort({weight:-1})

Dex gives me suggestion:

db['features'].ensureIndex({'active': 1, 'weight': 1}, {'background': true})

While I thought I should create index on weight descend:

db['features'].ensureIndex({'active': 1, 'weight': -1}, {'background': true})

I have post a question on Stackoverflow
in more details, would you please check it out?

shoujs commented Aug 24, 2012

after reading the answers in stackoverflow, i understand that the index works for both ascending and descending cases.

@shoujs shoujs closed this Aug 24, 2012
esedor commented Sep 5, 2012


Thanks for your question. There are a few situations where the order does matter, such as when you have a compound orderby clause combined with other factors (such as a query, with a return limit, on field A, ordered by { B: -1, C: 1}). For this and similar examples, an index on { A: 1, B: -1, C: 1 } should perform slightly more quickly and with slightly less memory used, because of a lefthanded optimization within the MongoDB index structure.

Right now, Dex blindly recommends ascending order for all indexes.

For your specific index, there is indeed not much difference in the order chosen. That said, I'd like to explore ways to include something about this in Dex's heuristics for generating recommendations!

@esedor esedor reopened this Sep 5, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment