When querying for documents in which a field does not exist, the optimiser will consider using a sparse index on that field, but this means that it will never find anything.
> db.test.insert({});
> db.test.insert({});
> db.test.insert({});
> db.test.insert({});
> db.test.insert({});
> db.test.find({foo: {$exists: false}}).count();
5
> db.test.ensureIndex(
,
{sparse: true, safe: true});
> db.test.find({foo: {$exists: false}}).count();
0
> db.test.find({foo: {$exists: false}}).explain();
{
"cursor" : "BtreeCursor foo_1",
"nscanned" : 0,
"nscannedObjects" : 0,
"n" : 0,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" :
}
- is duplicated by
-
SERVER-4578 $exists and sparse index
- Closed
-
SERVER-4666 sparse indexes break $exists query
- Closed