-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.0.2
-
Component/s: Index Maintenance
-
Query Optimization
I've seen Issue 2028 (https://jira.mongodb.org/browse/SERVER-2028) and I don't think a sparse index is a good enough solution. One of the reasons I think a sparse index fails as a solution is because when the index is used in a lookup, the documents that do have null values in the indexed field are ignored in search results. Let me give a concrete example of what I think the behavior should be...
Let's say I'm adding a new property to a "users" collection. The new property is an api token, and needs to be unique across the entire collection. I'd like to be able to do db.users.ensureIndex(
{"token": 1, "unique": true}), but this will of course fail b/c mongo will hit more than one null value. On the other hand, a sparse index here doesn't work because when I try to do a read query on that column where that column would get used (db.users.find({token: {"$ne": null}}).count) the result set excludes all the users b/c it's a new property.