Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-3368

Use index on array when querying on a a specific position within the array.

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Query Optimization

      Multikey arrays are used if you are querying for an array element as:

          db.foo.find({path: 10}).explain()
          {
      	"cursor" : "BtreeCursor path_1",
      	"nscanned" : 275271,
      	"nscannedObjects" : 275271,
      	"n" : 275271,
      	"millis" : 269,
      	"nYields" : 0,
      	"nChunkSkips" : 0,
      	"isMultiKey" : true,
      	"indexOnly" : false,
      	"indexBounds" : {
      		"path" : [
      			[
      				10,
      				10
      			]
      		]
      	}
          }
      

      But if you want to specifically find an array element at a specific position, the array is not used:

          db.foo.find({"path.0": 10}).explain()
          {
      	"cursor" : "BasicCursor",
      	"nscanned" : 2194200,
      	"nscannedObjects" : 2194200,
      	"n" : 80466,
      	"millis" : 1685,
      	"nYields" : 0,
      	"nChunkSkips" : 0,
      	"isMultiKey" : false,
      	"indexOnly" : false,
      	"indexBounds" : {
      		
      	}
          }
      

      This collection contains about 2.2 million documents with field "path" set to an array of 1 to 5 elements selected at random in range 0-50

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            dcrosta Daniel Crosta
            Votes:
            3 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated: