【发布时间】:2012-08-24 23:42:02
【问题描述】:
我想查看 MongoDB 使用的现有索引。我可以做相当于
$ mongod
> use my_db
> db.system.indexes.find()
使用 Mongoid?
$ rails console
> ?
在我使用 MongoHQ 的 heroku 应用程序中会很方便。谢谢!
【问题讨论】:
我想查看 MongoDB 使用的现有索引。我可以做相当于
$ mongod
> use my_db
> db.system.indexes.find()
使用 Mongoid?
$ rails console
> ?
在我使用 MongoHQ 的 heroku 应用程序中会很方便。谢谢!
【问题讨论】:
您可以通过其collection 获取 Mongoid 模型的底层索引。
> YourModel.collection.indexes
这会向下延伸到轻便摩托车司机(在 Mongoid 3 中)。见http://mongoid.org/en/moped/docs/driver.html
【讨论】:
Order.collection.indexes.each {|i| puts i.inspect};false 产生可读的结果
YourModel.collection.indexes.to_a 好像够用了。
为了使用史蒂夫的答案,我将它添加到一个模块中以做“常见的事情”:
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}]
【讨论】: