@kyanny's blog

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

MongoDB embedded fields への index のはり方

MongoMapper だと Hash で表現するので key そのものにインデックスをはれば Hash 全体に効くのかとおもいきや、違って、明示的に dot notation で指定しないとダメだった。

Single Field Indexes — MongoDB Manual 3.2

https://github.com/kyanny/playground/blob/gh-pages/mongo-index-hash/app.rb

class Foo
  include MongoMapper::Document
  key :h, Hash
  ensure_index 'h.a'
end
pp Foo.where('h.a' => 1).explain
{"queryPlanner"=>
  {"plannerVersion"=>1,
   "namespace"=>"testing.foos",
   "indexFilterSet"=>false,
   "parsedQuery"=>{"h.a"=>{"$eq"=>1}},
   "winningPlan"=>
    {"stage"=>"COLLSCAN",
     "filter"=>{"h.a"=>{"$eq"=>1}},
     "direction"=>"forward"},
   "rejectedPlans"=>[]},
 "executionStats"=>
  {"executionSuccess"=>true,
   "nReturned"=>1,
   "executionTimeMillis"=>0,
   "totalKeysExamined"=>0,
   "totalDocsExamined"=>1,
   "executionStages"=>
    {"stage"=>"COLLSCAN",
     "filter"=>{"h.a"=>{"$eq"=>1}},
     "nReturned"=>1,
     "executionTimeMillisEstimate"=>0,
     "works"=>3,
     "advanced"=>1,
     "needTime"=>1,
     "needYield"=>0,
     "saveState"=>0,
     "restoreState"=>0,
     "isEOF"=>1,
     "invalidates"=>0,
     "direction"=>"forward",
     "docsExamined"=>1},
   "allPlansExecution"=>[]},
 "serverInfo"=>
  {"host"=>"NagaeKensuke-no-MacBook-Pro.local",
   "port"=>27017,
   "version"=>"3.2.0",
   "gitVersion"=>"45d947729a0315accb6d4f15a6b06be6d9c19fe7"}}
------------------------
{"queryPlanner"=>
  {"plannerVersion"=>1,
   "namespace"=>"testing.foos",
   "indexFilterSet"=>false,
   "parsedQuery"=>{"h.a"=>{"$eq"=>1}},
   "winningPlan"=>
    {"stage"=>"COLLSCAN",
     "filter"=>{"h.a"=>{"$eq"=>1}},
     "direction"=>"forward"},
   "rejectedPlans"=>[]},
 "executionStats"=>
  {"executionSuccess"=>true,
   "nReturned"=>1,
   "executionTimeMillis"=>0,
   "totalKeysExamined"=>0,
   "totalDocsExamined"=>1,
   "executionStages"=>
    {"stage"=>"COLLSCAN",
     "filter"=>{"h.a"=>{"$eq"=>1}},
     "nReturned"=>1,
     "executionTimeMillisEstimate"=>0,
     "works"=>3,
     "advanced"=>1,
     "needTime"=>1,
     "needYield"=>0,
     "saveState"=>0,
     "restoreState"=>0,
     "isEOF"=>1,
     "invalidates"=>0,
     "direction"=>"forward",
     "docsExamined"=>1},
   "allPlansExecution"=>[]},
 "serverInfo"=>
  {"host"=>"NagaeKensuke-no-MacBook-Pro.local",
   "port"=>27017,
   "version"=>"3.2.0",
   "gitVersion"=>"45d947729a0315accb6d4f15a6b06be6d9c19fe7"}}
------------------------
{"queryPlanner"=>
  {"plannerVersion"=>1,
   "namespace"=>"testing.foos",
   "indexFilterSet"=>false,
   "parsedQuery"=>{"h.a"=>{"$eq"=>1}},
   "winningPlan"=>
    {"stage"=>"FETCH",
     "inputStage"=>
      {"stage"=>"IXSCAN",
       "keyPattern"=>{"h.a"=>1},
       "indexName"=>"h.a_1",
       "isMultiKey"=>false,
       "isUnique"=>false,
       "isSparse"=>false,
       "isPartial"=>false,
       "indexVersion"=>1,
       "direction"=>"forward",
       "indexBounds"=>{"h.a"=>["[1, 1]"]}}},
   "rejectedPlans"=>[]},
 "executionStats"=>
  {"executionSuccess"=>true,
   "nReturned"=>1,
   "executionTimeMillis"=>0,
   "totalKeysExamined"=>1,
   "totalDocsExamined"=>1,
   "executionStages"=>
    {"stage"=>"FETCH",
     "nReturned"=>1,
     "executionTimeMillisEstimate"=>0,
     "works"=>2,
     "advanced"=>1,
     "needTime"=>0,
     "needYield"=>0,
     "saveState"=>0,
     "restoreState"=>0,
     "isEOF"=>1,
     "invalidates"=>0,
     "docsExamined"=>1,
     "alreadyHasObj"=>0,
     "inputStage"=>
      {"stage"=>"IXSCAN",
       "nReturned"=>1,
       "executionTimeMillisEstimate"=>0,
       "works"=>2,
       "advanced"=>1,
       "needTime"=>0,
       "needYield"=>0,
       "saveState"=>0,
       "restoreState"=>0,
       "isEOF"=>1,
       "invalidates"=>0,
       "keyPattern"=>{"h.a"=>1},
       "indexName"=>"h.a_1",
       "isMultiKey"=>false,
       "isUnique"=>false,
       "isSparse"=>false,
       "isPartial"=>false,
       "indexVersion"=>1,
       "direction"=>"forward",
       "indexBounds"=>{"h.a"=>["[1, 1]"]},
       "keysExamined"=>1,
       "dupsTested"=>0,
       "dupsDropped"=>0,
       "seenInvalidated"=>0}},
   "allPlansExecution"=>[]},
 "serverInfo"=>
  {"host"=>"NagaeKensuke-no-MacBook-Pro.local",
   "port"=>27017,
   "version"=>"3.2.0",
   "gitVersion"=>"45d947729a0315accb6d4f15a6b06be6d9c19fe7"}}
------------------------