In previous version of mongo a sparse index on a field could not be used to find those documents that only had the indexed field. Take the following collection:
db.foo.ensureIndex({bar: 1}, {sparse: 1})
If one were to naively write a query to find all documents that had the "bar" property one would start with:
db.foo.find({bar: {$exists: true}})
In mongo 2.4 and below this particular query would result in a full collection scan with a BasicCursor. For an application in which "bar" would never be null this particular query could be rewritten as:
db.foo.find({bar: {$ne: null}})
This query would use the sparse index. It appears that while mongo 2.5 can now correctly use the sparse index for the $exists: true query it will fail to use any index for the {$ne: null} variety unless an explicit hint is given.
- related to
-
SERVER-13986 Creating a sparse index makes the not equals query to return diferent results.
- Closed