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

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

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • None
    • 1.6.5
    • None
    • None
    • 1.6.5 and 1.7.6
    • ALL

    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

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: