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

Creating _id index with unique:false should result in error

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.7.5
    • Affects Version/s: 2.4.10, 2.6.1
    • Component/s: Index Maintenance
    • Labels:
      None
    • ALL
    • Hide

      // Create a on unique index

      use foo
      db.createCollection("bar",

      {capped: false, autoIndexId: false}

      )
      db.bar.getIndexes()
      // Should return [ ]
      db.bar.ensureIndex({_id:1},

      {unique:false}

      )
      // index on _id will be returned
      db.bar.getIndexes()
      db.bar.insert(

      {"_id" : 1}

      )
      // The insert here will fail
      db.bar.insert(

      {"_id" : 1}

      )
      db.dropDatabase()

      // now the same thing, only the index is not on the id

      use foo
      db.createCollection("bar",

      {capped: false, autoIndexId: false}

      )
      db.bar.ensureIndex(

      {hash:1}

      ,

      {unique:false}

      )
      // index on hash will be returned
      db.bar.getIndexes()
      db.bar.insert(

      {"hash" : 1}

      )
      // The insert here will succeed
      db.bar.insert(

      {"hash" : 1}

      )
      db.dropDatabase()

      // now the same thing without an index

      use foo
      db.createCollection("bar",

      {capped: false, autoIndexId: false}

      )
      db.bar.insert(

      {"_id" : 1}

      )
      // The insert here will succeed
      db.bar.insert(

      {"_id" : 1}

      )
      db.dropDatabase()

      Show
      // Create a on unique index use foo db.createCollection("bar", {capped: false, autoIndexId: false} ) db.bar.getIndexes() // Should return [ ] db.bar.ensureIndex({_id:1}, {unique:false} ) // index on _id will be returned db.bar.getIndexes() db.bar.insert( {"_id" : 1} ) // The insert here will fail db.bar.insert( {"_id" : 1} ) db.dropDatabase() // now the same thing, only the index is not on the id use foo db.createCollection("bar", {capped: false, autoIndexId: false} ) db.bar.ensureIndex( {hash:1} , {unique:false} ) // index on hash will be returned db.bar.getIndexes() db.bar.insert( {"hash" : 1} ) // The insert here will succeed db.bar.insert( {"hash" : 1} ) db.dropDatabase() // now the same thing without an index use foo db.createCollection("bar", {capped: false, autoIndexId: false} ) db.bar.insert( {"_id" : 1} ) // The insert here will succeed db.bar.insert( {"_id" : 1} ) db.dropDatabase()

      When creating an index on an the _id with unique:false, the uniqueness is still enforced on it.

            Assignee:
            rassi J Rassi
            Reporter:
            levyavi Avi Levy
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: