Can I specify which index should use the query in Mongoid?

MongoDB seems to use an inefficient query pattern when one index is a subset of another index.

class Model
  field :status, :type => Integer
  field :title, :type => String
  field :subtitle, :type => String
  field :rating, :type => Float

  index([
    [:status, Mongo::ASCENDING],
    [:title, Mongo::ASCENDING],
    [:subtitle, Mongo::ASCENDING],
    [:rating, Mongo::DESCENDING]
  ])
  index([
    [:status, Mongo::ASCENDING],
    [:title, Mongo::ASCENDING],
    [:rating, Mongo::DESCENDING]
  ])
end

The first index is used when querying on status, titleand subtitleboth sorting by ratingand when querying only on statusand titleand sorting by rating, although using javascript explain()together with hint()the console states that using the second index is 4 times faster.

How can I tell Mongoid to tell MongoDB to use the second index?

+5
source share
2 answers

, Mongo::Collection, Mongoid::Criterion::Optional.extras

:

criteria = Model.where(:status => true, :title => 'hello world').desc(:rating)
criteria.extras(:hint => {:status => 1, :title => 1, :rating => -1})

extras , Mongo:: Collection

+7
0

All Articles