MongoDB: sharded systems cannot enforce cluster-wide unique indexes

66 views
Skip to first unread message

Andrea Zonzin

unread,
Mar 14, 2014, 5:53:59 AM3/14/14
to mongod...@googlegroups.com

Hi

from the mongoDB docs (http://docs.mongodb.org/manual/core/sharding-shard-key/) I'm reading

sharded systems cannot enforce cluster-wide unique indexes unless the unique field is in the shard key.

I'm not shure to understand well. If I have the following unique index {a,b,c,d} and the sharded key {a,b},

is the uniqueness of {a,b,c,d} respected across all the cluster?


The docs also says:

MongoDB does not support unique indexes across shards, except when the unique index 
contains the full shard key as a prefix of the index. In these situations MongoDB 
will enforce uniqueness across the full key, not a single field.

My doubts come from the contradictions of the following phrases from the documentation:
  1. "sharded systems cannot enforce cluster-wide unique indexes unless the unique field is in the shard key."
  2. "MongoDB does not support unique indexes across shards, except when the unique index contains the full shard key as a prefix of the index In the first case the index is in the shared key. In the second case the index contains the shared key. "
The one is the opposite of the other! 

Glenn Maynard

unread,
Mar 14, 2014, 3:58:21 PM3/14/14
to mongod...@googlegroups.com
#1 is a bit imprecise.  #2 is correct.

If your shard key is (a,b), then you can have a unique index (a,b,c,d).  That's because at any given time there's only a single shard where any particular (a,b,c,d) value can live on, so that single shard can enforce uniqueness for the subset of the data that lives on it.

(MongoDB could probably handle any unique index which is a superset of the shard key.  For example, the unique index (a,b,c,d) could probably be supported for shard keys (b,a) and (c,d), since there's still only one shard that any given (a,b,c,d) value can live on.  I don't think it currently allows this and requires that the shard key be an exact prefix of the unique index.)

--
Glenn Maynard

Reply all
Reply to author
Forward
0 new messages