加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 是否可以指定查询在Mongoid中应使用的索引?

发布时间:2020-12-17 03:26:50 所属栏目:百科 来源:网络整理
导读:当一个索引是另一个索引的子集时,MongoDB似乎使用了低效的查询模式. class Model field :status,:type = Integer field :title,:type = String field :subtitle,:type = String field :rating,:type = Float index([ [:status,Mongo::ASCENDING],[:title,[:s
当一个索引是另一个索引的子集时,MongoDB似乎使用了低效的查询模式.

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

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

在查询状态,标题和副标题以及对评级进行排序时以及在查询状态和标题以及对评级进行排序时都使用第一个索引,即使在javascript控制台中使用explain()和hint()也表示使用第二个指数快4倍.

我如何告诉Mongoid告诉MongoDB使用第二个索引?

解决方法

您可以使用Mongoid :: Criterion :: Optional.extras将选项(如提示)传递给Mongo :: Collection

一个例子:

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

extras接受Mongo::Collection可以处理的任何内容

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读