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

"unique: true" in shardcollection command does not check that the index used has been created as unique

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.9.0
    • Affects Version/s: None
    • Component/s: Sharding
    • None
    • ALL

      The uniqueness of the key used as shard key is enforced by:
      1) sharding makes sure a given shard key value can only live in 1 shard.
      2) then within each shard, the unique index on key

      I'm wondering what the "unique: true" actually does when calling
      shardcollection.
      I would think that it just checks that #2 is valid.
      But according to my tests, it wont create the indexes by itself, nor
      force uniqueness on it:

      > db.runCommand( { shardcollection : "mydb.fs.files", key :

      { filename: 1 }

      , unique: true })
      {
      "ok" : 0,
      "errmsg" : "please create an index over the sharding key before sharding."
      }
      > use mydb
      switched to db mydb
      > db.fs.files.ensureIndex(

      {filename:1}

      )
      > use admin
      switched to db admin
      > db.runCommand( { shardcollection : "mydb.fs.files", key :

      { filename: 1 }

      , unique: true })

      { "collectionsharded" : "mydb.fs.files", "ok" : 1 }

      > use mydb
      switched to db mydb
      > db.fs.files.getIndexes()
      [
      {
      "name" : "id",
      "ns" : "mydb.fs.files",
      "key" :

      { "_id" : 1 }

      ,
      "v" : 0
      },
      {
      "_id" : ObjectId("4d228a8197d965fe1d2aaf34"),
      "ns" : "mydb.fs.files",
      "key" :

      { "filename" : 1 }

      ,
      "name" : "filename_1",
      "v" : 0
      }
      ]

      this turns out to be a bug: the shardcollection command should fail if it was called with "unique: true" but the existing index used is not unique.

            Assignee:
            greg_10gen Greg Studer
            Reporter:
            antoine Antoine Girbal
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: