|
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
|
|