[SERVER-3685] Unique index with sparse does not work Created: 25/Aug/11 Updated: 03/Nov/14 Resolved: 25/Aug/11 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Index Maintenance |
| Affects Version/s: | 1.8.2 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Tanin Na Nakorn | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows 7 |
||
| Operating System: | ALL |
| Participants: |
| Description |
|
In my mongo shell, I do this: db.members.ensureIndex( {facebook_id:1}, {unique:true,sparse:true})
Then I try to insert 2 records with facebook_id = null I have got this message: E11000 duplicate key error index: writepub.members.$facebook_id_1 dup key: { :null } |
| Comments |
| Comment by kic [ 03/Nov/14 ] |
|
That is what is sparse for: Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value. The index skips over any document that is missing the indexed field. http://docs.mongodb.org/manual/core/index-sparse/#index-type-sparse |
| Comment by Eliot Horowitz (Inactive) [ 25/Aug/11 ] |
|
null counts as being a field. |
| Comment by Scott Hernandez (Inactive) [ 25/Aug/11 ] |
|
(tested on 1.9.2) > db.users.dropIndexes() > db.users.ensureIndex( {facebook_id:true}, {sparse:true, unique:true}) ).explain() } , , ---------------------- > db.users.remove() |