|
Sparse indexes can generally be used to answer queries that include a "non-sparse" predicate on an indexed field.
This is not the case, however, for query plans that perform a whole index scan to provide a sort. These query plans should be able to use sparse indexes if a "non-sparse" predicate is given on an indexed field.
> db.foo.dropIndexes()
|
{
|
"nIndexesWas" : 2,
|
"msg" : "non-_id indexes dropped for collection",
|
"ok" : 1
|
}
|
> db.foo.ensureIndex({a:1,b:1},{sparse:true})
|
{
|
"createdCollectionAutomatically" : true,
|
"numIndexesBefore" : 1,
|
"numIndexesAfter" : 2,
|
"ok" : 1
|
}
|
> db.foo.find({a:1,b:1}).sort({a:1}).explain().cursor
|
BtreeCursor a_1_b_1 // Bounded index scan; sparse index is used.
|
> db.foo.find({b:1}).sort({a:1}).explain().cursor
|
BasicCursor // Whole index scan; sparse index not used.
|
|