Multiple unique keys and sharded keys

21 views
Skip to first unread message

stefanoric

unread,
Oct 4, 2012, 10:57:08 AM10/4/12
to mongod...@googlegroups.com
Hi,

I have a collection of "users" with 2 unique indexes (username and email).

I tried sharding this collection on username, but mongo complains about not being able to maintain uniqueness.

I think I understand why I said that (might end up with two users with the same email in 2 different shards), so I tried to sharding using both username and email as shard key, but still mongo complains.

Is there no workaround to this situation? Should I drop the uniqueness on username and email and maintain it programmatically?

Thanks,
 Stefano R.


Jenna deBoisblanc

unread,
Oct 5, 2012, 3:32:32 PM10/5/12
to mongodb-user
Hello Stefano,

Consider the following documents, which will have separate, unique
index entries in the compound index, {user: 1, email: 1}:
{user: "Jenna", email: "y"}
{user: "Jenna", email: "x"}

A compound index cannot be used to maintain uniqueness on two distinct
fields; it can only be used to ensure uniqueness for the combination
of values. Dropping the uniqueness on a single field and maintaining
uniqueness programmatically is workaround for this situation.

On Oct 4, 10:57 am, stefanoric <stefano.riccia...@gmail.com> wrote:
> Hi,
>
> I have a collection of "users" with 2 unique indexes (username and email).
>
> I tried sharding this collection on username, but mongo complains about not
> being able to maintain uniqueness.
>
> I think I understand why I said that (might end up with two users with the
> same email in 2 different shards), so I tried to sharding using *both *username
> *and *email as shard key, but still mongo complains.
Reply all
Reply to author
Forward
0 new messages