【问题标题】:See existing indexes in MongoDB using mongoid使用 mongoid 查看 MongoDB 中的现有索引
【发布时间】:2012-08-24 23:42:02
【问题描述】:

我想查看 MongoDB 使用的现有索引。我可以做相当于

$ mongod
> use my_db
> db.system.indexes.find()

使用 Mongoid?

$ rails console
> ?

在我使用 MongoHQ 的 heroku 应用程序中会很方便。谢谢!

【问题讨论】:

    标签: mongoid indexing


    【解决方案1】:

    您可以通过其collection 获取 Mongoid 模型的底层索引。

    > YourModel.collection.indexes
    

    这会向下延伸到轻便摩托车司机(在 Mongoid 3 中)。见http://mongoid.org/en/moped/docs/driver.html

    【讨论】:

    • 谢谢。 Order.collection.indexes.each {|i| puts i.inspect};false 产生可读的结果
    • Order.collection.indexes.to_a 更容易产生可读的结果
    • YourModel.collection.indexes.to_a 好像够用了。
    【解决方案2】:

    为了使用史蒂夫的答案,我将它添加到一个模块中以做“常见的事情”:

    module CommonModelMethods
      extend ActiveSupport::Concern
      class_methods do
        def show_indexes
          self.collection.indexes.to_a.collect{|i| i[:key]}
        end
      end
    end
    

    然后可以将该模块包含在您的类中(在开发过程中很有用):

    class WaterSupply
      include Mongoid::Document
      include CommonModelMethods
      ...
    end
    

    这可能会在控制台中产生类似的结果:

    2.4.5 :031 > WaterSupply.show_indexes
     => [{"_id"=>1}, {"location"=>"2dsphere"}, {"address"=>1}, {"organization_id"=>1, "address"=>1}] 
    

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 2013-07-31
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 2016-04-27
      相关资源
      最近更新 更多