Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-7578

Enforcing Unique Keys for Sharded Collections not accurate.

    • Type: Icon: Task Task
    • Resolution: Won't Do
    • Priority: Icon: Major - P3 Major - P3
    • Server_Docs_20231030
    • Affects Version/s: None
    • Component/s: Server
    • Labels:
      None
    • 2.5

      The documentation states
      "MongoDB does not support creating new unique indexes in sharded collections and will not allow you to shard collections with unique indexes on fields other than the _id field."

      But I can do:

      MongoDB Enterprise mongos> db.testColUniq5.insert({"email":"a"})
      WriteResult({ "nInserted" : 1 })
      MongoDB Enterprise mongos> db.testColUniq5.createIndex({"email":1},{unique:1})
      {
      	"raw" : {
      		"Shakirs-MacBook-Pro.local:27018" : {
      			"createdCollectionAutomatically" : false,
      			"numIndexesBefore" : 1,
      			"numIndexesAfter" : 2,
      			"ok" : 1
      		}
      	},
      	"ok" : 1
      }
      MongoDB Enterprise mongos> sh.shardCollection("test.testColUniq5", {"email":1},{unique:true})
      { "collectionsharded" : "test.testColUniq5", "ok" : 1 }
      MongoDB Enterprise mongos> 
      

      Further... it states I should not be able to create new unique indexes BUT I can and it's broken:

      MongoDB Enterprise mongos> db.testColUniq5.createIndex({"email3":1},{unique:1})
      {
      	"raw" : {
      		"Shakirs-MacBook-Pro.local:27018" : {
      			"createdCollectionAutomatically" : false,
      			"numIndexesBefore" : 2,
      			"ok" : 0,
      			"errmsg" : "cannot create unique index over { email3: 1.0 } with shard key pattern { email: 1.0 }",
      			"code" : 67
      		},
      		"Shakirs-MacBook-Pro.local:27019" : {
      			"createdCollectionAutomatically" : false,
      			"numIndexesBefore" : 2,
      			"numIndexesAfter" : 3,
      			"ok" : 1
      		},
      		"Shakirs-MacBook-Pro.local:27020" : {
      			"createdCollectionAutomatically" : false,
      			"numIndexesBefore" : 2,
      			"numIndexesAfter" : 3,
      			"ok" : 1
      		}
      	},
      	"code" : 67,
      	"ok" : 0,
      	"errmsg" : "{ Shakirs-MacBook-Pro.local:27018: \"cannot create unique index over { email3: 1.0 } with shard key pattern { email: 1.0 }\" }"
      }
      

      Further, if you go to the individual shards, 2 of 3 actually have the new unique index added. Is this a bug?

            Assignee:
            ravind.kumar Ravind Kumar (Inactive)
            Reporter:
            shakir.sadikali@mongodb.com Shakir Sadikali
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              1 year, 6 weeks ago