Skip to content

Commit b1d8195

Browse files
committed
add clarification to unique _id index if not part of shard key
1 parent b44765d commit b1d8195

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

source/core/sharding-shard-key.txt

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

94-
For a sharded collection, only the ``_id`` field index and the index on
95-
the shard key or a :term:`compound index` where the shard key is a
96-
:ref:`prefix <compound-index-prefix>` can be :doc:`unique
97-
</core/index-unique>`:
94+
For a ranged sharded collection, only the following indexes can be
95+
:doc:`unique </core/index-unique>`:
9896

99-
- You cannot shard a collection that has unique indexes on other fields.
97+
- the index on the shard key
10098

101-
- You cannot create unique indexes on other fields for a sharded
102-
collection.
99+
- a :term:`compound index` where the shard key is a :ref:`prefix
100+
<compound-index-prefix>`
101+
102+
- the default ``_id`` index
103+
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.
109+
110+
For example, consider a sharded collection (with shard key ``{x:
111+
1}``) that spans two shards A and B. Because the ``_id`` key is
112+
not part of the shard key, the collection could have a document
113+
with ``_id`` value ``1`` in shard A and another document with
114+
``_id`` value ``1`` in shard B.
115+
116+
The unique index constraints mean that:
117+
118+
- For a to-be-sharded collection, you cannot shard the collection if
119+
the collection has other unique indexes.
120+
121+
- For an already-sharded collection, you cannot create unique indexes
122+
on other fields.
103123

104124
Through the use of the unique index on the shard key, MongoDB *can*
105125
enforce uniqueness on the shard key values. MongoDB enforces uniqueness

0 commit comments

Comments
 (0)