Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-2580

ensureIndex with unique:true and dropDups:true fails if duplicate data present

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.6.5
    • Component/s: None
    • None
    • Environment:
      1.6.5 and 1.7.6
    • ALL

      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

            Assignee:
            Unassigned Unassigned
            Reporter:
            alvin Alvin Richards (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: