[SERVER-2580] ensureIndex with unique:true and dropDups:true fails if duplicate data present Created: 17/Feb/11  Updated: 02/Mar/11  Resolved: 02/Mar/11

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 1.6.5
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Alvin Richards (Inactive) Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

1.6.5 and 1.7.6


Issue Links:
Depends
Duplicate
duplicates SERVER-2054 Creating an index with dropDups shoul... Closed
Operating System: ALL
Participants:

 Description   

Problem:

From the documentation

http://www.mongodb.org/display/DOCS/Indexes

A unique index cannot be created on a key that has duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate values, add the dropDups option.

db.things.ensureIndex(

{firstname : 1}

,

{unique : true, dropDups : true}

)

However, this appears to be broken, since its errors out with

E11000 duplicate key error index: test.fb.$name_1 dup key: { : "fred" }

Reproduce:

> db.fb.save(

{name:"fred"}

);
> db.fb.save(

{name:"fred"}

);
> db.fb.save(

{name:"fred"}

);
> db.fb.ensureIndex(

{name:1}

,

{unique:1, dropDupes:1}

);
E11000 duplicate key error index: test.fb.$name_1 dup key: { : "fred" }
> db.fb.ensureIndex(

{name:1}

,

{unique:true, dropDupes:true}

);
E11000 duplicate key error index: test.fb.$name_1 dup key: { : "fred" }

Business case:

  • broken functionality


 Comments   
Comment by Alvin Richards (Inactive) [ 17/Feb/11 ]

Duplicate issue of SERVER-2054

Comment by Eliot Horowitz (Inactive) [ 17/Feb/11 ]

You're probably just seeing: SERVER-2054

Comment by Eliot Horowitz (Inactive) [ 17/Feb/11 ]

Was the index created successfully?
You can check with
db.fb.getIndexes()

Comment by Alvin Richards (Inactive) [ 17/Feb/11 ]

Sorry, cut & pasted the wrong example

> db.fb.ensureIndex(

{name:1}

,

{unique:1, dropDups:1}

);
E11000 duplicate key error index: test.fb.$name_1 dup key: { : "fred" }
>

> db.fb.ensureIndex(

{name:1}

,

{unique:true, dropDups:true}

);
E11000 duplicate key error index: test.fb.$name_1 dup key: { : "fred" }
>

Comment by Eliot Horowitz (Inactive) [ 17/Feb/11 ]

Type, dropDups vs dropDupes
Documentation is correct.

Generated at Thu Feb 08 03:00:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.