How to precisely control your queries with MongoMapper
I have an index like this in my database (for my table entries): {"created_at": -1, "search_id": 1, "services":1}
I can make use of that index if I perform a search in the Mongo console lik开发者_JAVA百科e this:
db.entries.find({'search_id':1, 'services':2}).sort({'created_at':
-1})
(if I perform an explain() on that query I can see that it's using the index).
However I can't replicate that behavior with MongoMapper. I've tried with a search like this:
Entry.where(:search_id => 1, :services => 2).order(['created_at',
'descending'])
I've altered the order and tried everything, but when I perform an explain on it I get that it's using a BasicCursor (that is, no index) and if I do a "criteria" on that query I can see that it's not even trying to order the results in the search: I guess they are ordered afterwards once the results are back.
Is there a way to control your queries more precisely so you can make use of an index in cases like these?
My bad guys, I was using the order
function incorrectly as this time it expects a symbol rather than an array:
Entry.where(:search_id => 1, :services => 2).order(:created_at.desc)
精彩评论