[SERVER-2326] "unique: true" in shardcollection command does not check that the index used has been created as unique Created: 04/Jan/11  Updated: 12/Jul/16  Resolved: 30/Mar/11

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: None
Fix Version/s: 1.9.0

Type: Bug Priority: Major - P3
Reporter: Antoine Girbal Assignee: Greg Studer
Resolution: Done Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Operating System: ALL
Participants:

 Description   

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.



 Comments   
Comment by auto [ 30/Aug/11 ]

Author:

{u'login': u'stbrody', u'name': u'Spencer T Brody', u'email': u'spencer@10gen.com'}

Message: Add more tests for SERVER-2326 & SERVER-3718.
Branch: master
https://github.com/mongodb/mongo/commit/ad830c9c07b7e99187719ae8d099835fb684d0a4

Comment by auto [ 30/Mar/11 ]

Author:

{u'login': u'gregstuder', u'name': u'gregs', u'email': u'greg@10gen.com'}

Message: fix for SERVER-2326, unique : true semantics
Branch: master
https://github.com/mongodb/mongo/commit/69cf8bee0b7ae3135560a1baa5d7f292995129c6

Comment by Lexi Pimenidis [ 13/Jan/11 ]

BTW, I'm just about to enable sharding on a 230GB-database, actually trying to use the unique-option.

Comment by Eliot Horowitz (Inactive) [ 04/Jan/11 ]

Most times people enablesharding its empty, so that's definitely correct.

Comment by Antoine Girbal [ 04/Jan/11 ]

Right now, the only case where "unique: true" works is when it is called on an empty collection, in which case the index gets created automatically.
To me, it would be cleaner and easier to maintain if shardcollection always relied on a preexisting index, and did not have "unique" option.
This way we dont have to duplicate options or have inconsistency between ensureIndex and shardCollection.
Having it work only when collection is empty is not very useful.

Generated at Thu Feb 08 02:59:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.