@kyanny's blog

My thoughts, my life. Views/opinions are my own.

MongoMapper + mongo-ruby-driver v1.x でコレクションに定義されているインデックスの情報を得る

mongo-ruby-driver 1.12.1 とか。 index_information メソッドを使う。

Class: Mongo::Collection — mongo-1.12.1

gist.github.com

$ ruby mm_index_information.rb
{"_id_"=>{"v"=>1, "key"=>{"_id"=>1}, "ns"=>"testing.users", "name"=>"_id_"}}
{"_id_"=>
  {"v"=>1, "key"=>{"_id"=>1}, "ns"=>"testing.bookmarks", "name"=>"_id_"},
 "user_id_1"=>
  {"v"=>1,
   "key"=>{"user_id"=>1},
   "name"=>"user_id_1",
   "ns"=>"testing.bookmarks"}}
{"_id_"=>{"v"=>1, "key"=>{"_id"=>1}, "name"=>"_id_", "ns"=>"testing.tags"},
 "user_id_1"=>
  {"v"=>1, "key"=>{"user_id"=>1}, "name"=>"user_id_1", "ns"=>"testing.tags"},
 "bookmark_id_1"=>
  {"v"=>1,
   "key"=>{"bookmark_id"=>1},
   "name"=>"bookmark_id_1",
   "ns"=>"testing.tags"},
 "user_id_1_bookmark_id_1"=>
  {"v"=>1,
   "key"=>{"user_id"=>1, "bookmark_id"=>1},
   "name"=>"user_id_1_bookmark_id_1",
   "ns"=>"testing.tags"}}