Skip to content

Commit 0f0808c

Browse files
author
Kay Kim
committed
DOCS-10517: unique index and shard keys
1 parent b1d8195 commit 0f0808c

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

source/core/index-unique.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,46 @@ A partial index with a unique constraint does not prevent the insertion
220220
of documents that do not meet the unique constraint if the documents do
221221
not meet the filter criteria. For an example, see
222222
:ref:`partial-index-with-unique-constraints`.
223+
224+
Sharded Clusters and Unique Indexes
225+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226+
227+
You cannot specify a unique constraint on a :ref:`hashed index
228+
<index-type-hashed>`.
229+
230+
For a ranged sharded collection, only the following indexes can be
231+
:doc:`unique </core/index-unique>`:
232+
233+
- the index on the shard key
234+
235+
- a :term:`compound index` where the shard key is a :ref:`prefix
236+
<compound-index-prefix>`
237+
238+
- the default ``_id`` index; **however**, the ``_id`` index only
239+
enforces the uniqueness constraint per shard **if** the ``_id`` field
240+
is not the shard key or the prefix of the shard key.
241+
242+
.. admonition:: Uniqueness and the ``_id`` Index
243+
:class: important
244+
245+
If the ``_id`` field is not the shard key or the prefix of the
246+
shard key, ``_id`` index only enforces the uniqueness constraint
247+
per shard and **not** across shards.
248+
249+
For example, consider a sharded collection (with shard key ``{x:
250+
1}``) that spans two shards A and B. Because the ``_id`` key is
251+
not part of the shard key, the collection could have a document
252+
with ``_id`` value ``1`` in shard A and another document with
253+
``_id`` value ``1`` in shard B.
254+
255+
If the ``_id`` field is not the shard key nor the prefix of the
256+
shard key, MongoDB expects applications to enforce the uniqueness
257+
of the ``_id`` values across the shards.
258+
259+
The unique index constraints mean that:
260+
261+
- For a to-be-sharded collection, you cannot shard the collection if
262+
the collection has other unique indexes.
263+
264+
- For an already-sharded collection, you cannot create unique indexes
265+
on other fields.

source/core/sharding-shard-key.txt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ recreating an index on just the shard key.
9191
Unique Indexes
9292
~~~~~~~~~~~~~~
9393

94+
You cannot specify a unique constraint on a :ref:`hashed index
95+
<index-type-hashed>`.
96+
9497
For a ranged sharded collection, only the following indexes can be
9598
:doc:`unique </core/index-unique>`:
9699

@@ -99,20 +102,27 @@ For a ranged sharded collection, only the following indexes can be
99102
- a :term:`compound index` where the shard key is a :ref:`prefix
100103
<compound-index-prefix>`
101104

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
103111

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.
109115

110116
For example, consider a sharded collection (with shard key ``{x:
111117
1}``) that spans two shards A and B. Because the ``_id`` key is
112118
not part of the shard key, the collection could have a document
113119
with ``_id`` value ``1`` in shard A and another document with
114120
``_id`` value ``1`` in shard B.
115121

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+
116126
The unique index constraints mean that:
117127

118128
- For a to-be-sharded collection, you cannot shard the collection if
@@ -130,9 +140,6 @@ method:
130140

131141
.. include:: /includes/extracts/shard-collection-unique-restriction-method.rst
132142

133-
You cannot specify a unique constraint on a :ref:`hashed index
134-
<index-type-hashed>`.
135-
136143
.. index:: shard key; cardinality
137144

138145
.. _shard-key-selection-divisible:

0 commit comments

Comments
 (0)