@@ -91,6 +91,9 @@ recreating an index on just the shard key.
91
91
Unique Indexes
92
92
~~~~~~~~~~~~~~
93
93
94
+ You cannot specify a unique constraint on a :ref:`hashed index
95
+ <index-type-hashed>`.
96
+
94
97
For a ranged sharded collection, only the following indexes can be
95
98
:doc:`unique </core/index-unique>`:
96
99
@@ -99,20 +102,27 @@ For a ranged sharded collection, only the following indexes can be
99
102
- a :term:`compound index` where the shard key is a :ref:`prefix
100
103
<compound-index-prefix>`
101
104
102
- - the default ``_id`` index
105
+ - the default ``_id`` index; **however**, the ``_id`` index only
106
+ enforces the uniqueness constraint per shard **if** the ``_id`` field
107
+ is not the shard key or the prefix of the shard key.
108
+
109
+ .. admonition:: Uniqueness and the ``_id`` Index
110
+ :class: important
103
111
104
- .. note::
105
-
106
- If the ``_id`` field is not part of the shard key, ``_id`` index
107
- only enforces the uniqueness constraint per shard and **not**
108
- across shards.
112
+ If the ``_id`` field is not the shard key or the prefix of the
113
+ shard key, ``_id`` index only enforces the uniqueness constraint
114
+ per shard and **not** across shards.
109
115
110
116
For example, consider a sharded collection (with shard key ``{x:
111
117
1}``) that spans two shards A and B. Because the ``_id`` key is
112
118
not part of the shard key, the collection could have a document
113
119
with ``_id`` value ``1`` in shard A and another document with
114
120
``_id`` value ``1`` in shard B.
115
121
122
+ If the ``_id`` field is not the shard key nor the prefix of the
123
+ shard key, MongoDB expects applications to enforce the uniqueness
124
+ of the ``_id`` values across the shards.
125
+
116
126
The unique index constraints mean that:
117
127
118
128
- For a to-be-sharded collection, you cannot shard the collection if
@@ -130,9 +140,6 @@ method:
130
140
131
141
.. include:: /includes/extracts/shard-collection-unique-restriction-method.rst
132
142
133
- You cannot specify a unique constraint on a :ref:`hashed index
134
- <index-type-hashed>`.
135
-
136
143
.. index:: shard key; cardinality
137
144
138
145
.. _shard-key-selection-divisible:
0 commit comments