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

Behavior change with hint and sparse indexes

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 3.3.2
    • None
    • None
    • ALL

    Description

      With MongoDB <= 3.2 a query using a hint on an index where none of the documents are in the index returns results. The hint appears to be ignored:

      > db.test.insert({i: 1})
      WriteResult({ "nInserted" : 1 })
      > db.test.insert({i: 2})
      WriteResult({ "nInserted" : 1 })
      > db.test.createIndex({x: 1})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 1,
      	"numIndexesAfter" : 2,
      	"ok" : 1
      }
      > db.runCommand({count: 'test', filter: {}, hint: "x_1"})
      { "waitedMS" : NumberLong(0), "n" : 2, "ok" : 1 }
      > db.test.dropIndex("x_1")
      { "nIndexesWas" : 2, "ok" : 1 }
      > db.test.createIndex({x: 1}, {sparse: true})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 1,
      	"numIndexesAfter" : 2,
      	"ok" : 1
      }
      > db.runCommand({count: 'test', filter: {}, hint: "x_1"})
      { "waitedMS" : NumberLong(0), "n" : 2, "ok" : 1 }
      > db.version()
      3.2.3
      

      With MongoDB 3.3.2 and a sparse index the hint is no longer ignored:

      > db.test.createIndex({x: 1})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 1,
      	"numIndexesAfter" : 2,
      	"ok" : 1
      }
      > db.runCommand({count: 'test', filter: {}, hint: "x_1"})
      { "waitedMS" : NumberLong(0), "n" : 2, "ok" : 1 }
      > db.test.dropIndex("x_1")
      { "nIndexesWas" : 2, "ok" : 1 }
      > db.test.createIndex({x: 1}, {sparse: true})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 1,
      	"numIndexesAfter" : 2,
      	"ok" : 1
      }
      > db.runCommand({count: 'test', filter: {}, hint: "x_1"})
      { "waitedMS" : NumberLong(0), "n" : 0, "ok" : 1 }
      > db.version()
      3.3.2
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            bernie@mongodb.com Bernie Hackett
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: