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

Multi-key indexes with empty arrays may cause incorrect results on queries that don't use the full index

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.1.2
    • Affects Version/s: 1.0.0
    • Component/s: Index Maintenance
    • None
    • Environment:
      Any, git hash 644df81d2167628a7a9494a1953f981d148ebfbb

      If you create a multi-key index that has documents omitted because they have null values or empty arrays, mongo may return incorrect results on other queries that attempt to use part of that index. The query planner will use a multi-key index (take, for example, an index with keys: k0, k1) if you run a query that matches part of the index (for example, searching on k0). However, if some documents are missing because criteria unrelated to the current query causes them to be excluded from the index (for example, where k1 is an array, and the document has an empty array), then searching just on the partial index will not return all documents that should have matched.

      Repro follows:

      MongoDB shell version: 1.1.0-
      url: testdb
      connecting to: 127.0.0.1:27001/testdb
      type "help" for help
      > db.party.save(

      {id:1,info:[],str:"hi"}

      );
      > db.party.save(

      {id:1,info:["foo"],str:"hi"}

      );
      > db.party.find(

      {id:1,str:"hi"}

      );

      {"_id" : ObjectId( "4aa09b10fce2291061418fb1") , "id" : 1 , "info" : [] , "str" : "hi"} {"_id" : ObjectId( "4aa09b1bfce2291061418fb2") , "id" : 1 , "info" : ["foo"] , "str" : "hi"}

      > db.party.ensureIndex(

      {id:1,info:1}

      );
      true
      > db.party.find(

      {id:1,str:"hi"}

      );

      {"_id" : ObjectId( "4aa09b1bfce2291061418fb2") , "id" : 1 , "info" : ["foo"] , "str" : "hi"}

      > db.party.dropIndexes();

      {"nIndexesWas" : 2 , "msg" : "all indexes deleted for collection" , "ok" : 1}

      > db.party.ensureIndex(

      {id:1}

      );
      true
      > db.party.find(

      {id:1,str:"hi"}

      );

      {"_id" : ObjectId( "4aa09b10fce2291061418fb1") , "id" : 1 , "info" : [] , "str" : "hi"} {"_id" : ObjectId( "4aa09b1bfce2291061418fb2") , "id" : 1 , "info" : ["foo"] , "str" : "hi"}

            Assignee:
            eliot Eliot Horowitz (Inactive)
            Reporter:
            fes Frank Swiderski
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: