[SERVER-22943] Behavior change with hint and sparse indexes Created: 03/Mar/16  Updated: 03/Mar/16  Resolved: 03/Mar/16

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 3.3.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Bernie Hackett Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 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



 Comments   
Comment by Charlie Swanson [ 03/Mar/16 ]

This behavior change was intentional (see SERVER-22041).

There was an inconsistency where a find command would respect the hint, but a count command would not.

I've filed DOCS-7337 to make sure this gets into the compatibility notes.

Generated at Thu Feb 08 04:01:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.