Hashed and ranged index on same field

when create two indexes on a collection:
db.createIndex({“name”: “hashed”})
db.createIndex({“name”: 1}, {unique:true})
what is the behavior of this collection?
Actually, original problem is this:
first create an empty shard collection by : sh.shardCollection(“db”, {“name”: “hashed”})
which would create a hashed index on db with the field name, but also I want the field name to be unique
so I create another index by : db.createIndex({“name”:1}, {unique:true})
what’s the behavior of the collection?How the data of this collection arranged in shards, is it hashed or is it ranged?
(dont focos on the code, I omit the collection name , it’s just a simple example)

Hi @11115

Having a hashed and unique index on the same field is a valid strategy to have uniqueness enforced on a sharded collection, since:

  1. A value “x” will be hashed to another value, say “z”.
  2. Another value “y” can also be hashed to the same value of “z” (hash collision). This is fine for our purposes.
  3. You cannot enforce uniqueness on a hashed index.
  4. However if you create a unique index on the same field, you can enforce uniqueness on that field, in a sharded collection.

So using your example, you can shard the collection using ({“name”: “hashed”}) as the shard key to allow for semi-random distribution of documents across shards. However if you also would like to enforce that “name” should be unique, creating a unique index ({“name”: 1}, {unique:true}) should do that for you.

This is mentioned briefly in Hashed Index: unique constraint.

Best regards,


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.