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(
)
> use admin
switched to db admin
> db.runCommand( { shardcollection : "mydb.fs.files", key :
, 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" :
,
"v" : 0
},
{
"_id" : ObjectId("4d228a8197d965fe1d2aaf34"),
"ns" : "mydb.fs.files",
"key" :
,
"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.