-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsh.reshardCollection.txt
315 lines (226 loc) · 8.59 KB
/
sh.reshardCollection.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
======================
sh.reshardCollection()
======================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: sh.reshardCollection(namespace, key, unique, options)
.. versionadded:: 5.0
The :method:`sh.reshardCollection()` method changes the shard key for
a collection and changes the distribution of your data.
Before you reshard a collection, read the the :ref:`reshard
requirements <reshard-requirements>` and :ref:`reshard limitations
<resharding-limitations>`.
.. |dbcommand| replace:: :dbcommand:`reshardCollection` command
.. include:: /includes/fact-mongosh-shell-method-alt.rst
``sh.reshardCollection()`` takes the following fields:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Field
- Type
- Description
* - ``namespace``
- string
- The :term:`namespace` of the collection to shard in the form
``"<database>.<collection>"``.
* - ``key``
- document
- The document that specifies the new field or fields to use as the
:ref:`shard key <sharding-shard-key>`.
``{ <field1>: <1|"hashed">, ... }``
Set the field values to either:
- ``1`` for :ref:`range-based sharding <sharding-ranged>`
- ``"hashed"`` to specify a
:ref:`hashed shard key <hashed-shard-keys>`.
See also :ref:`sharding-shard-key-indexes`
* - ``unique``
- boolean
- Optional. Specify whether there is a :doc:`uniqueness
</core/index-unique>` constraint on the shard key. Only
``false`` is supported. Defaults to ``false``.
* - ``options``
- document
- Optional. A document containing optional fields, including
``numInitialChunks``, ``collation``, ``zones`` and
``forceRedistribution``.
The ``options`` field supports the following fields:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Field
- Type
- Description
* - ``numInitialChunks``
- integer
- Optional. Specifies the initial number of chunks to create
across all shards in the cluster when resharding a collection.
The default value is ``90``. MongoDB will then create and balance
chunks across the cluster. The ``numInitialChunks`` must result
in less than ``8192`` per shard.
.. include:: /includes/initial-chunks-recommendation.rst
* - ``collation``
- document
- Optional. If the collection specified to ``reshardCollection``
has a default :ref:`collation <collation>`, you *must* include a
collation document with ``{ locale : "simple" }``, or the
``reshardCollection`` command fails.
* - ``zones``
- array
- Optional. Specifies the zones for the collection.
To maintain or add :ref:`zones <zone-sharding>`,
specify the zones for your collection in an array:
.. code-block:: javascript
:copyable: false
[
{s
min: <document with same shape as shardkey>,
max: <document with same shape as shardkey>,
zone: <string> | null
},
...
]
* - ``forceRedistribution``
- boolean
- .. _forceRedistribution-option:
.. include:: /includes/fact-forceRedistribution-desc.rst
Compatibility
-------------
This method is available in deployments hosted in the following environments:
.. include:: /includes/fact-environments-atlas-only.rst
.. include:: /includes/fact-environments-atlas-support-no-free.rst
.. include:: /includes/fact-environments-onprem-only.rst
Considerations
--------------
.. include:: /includes/sharding/reshard-build-indexes-consideration.rst
.. _resharding-process-details:
Resharding Process
------------------
.. include:: /includes/reshard-collection-introduction.rst
Initialization Phase
~~~~~~~~~~~~~~~~~~~~
.. include:: /includes/reshard-collection-initialization.rst
Clone Phase
~~~~~~~~~~~
.. include:: /includes/reshard-collection-clone.rst
Index Phase
~~~~~~~~~~~
.. include:: /includes/reshard-collection-index.rst
Apply and Catch-up Phase
~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: /includes/reshard-collection-apply-and-catchup.rst
.. note::
If required, you can manually force the resharding operation to
complete by issuing the :method:`sh.commitReshardCollection()`
method. This is useful if the current time estimate to complete the
resharding operation is an acceptable duration for your collection
to block writes. The :method:`sh.commitReshardCollection()` method
blocks writes early and forces the resharding operation to
complete. During the time period where writes are blocked your
application experiences an increase in latency.
.. _resharding-commit-phase-method:
Commit Phase
~~~~~~~~~~~~
.. include:: /includes/reshard-collection-commit.rst
.. note::
Once the resharding process reaches the commit phase, the process
cannot be ended with :method:`sh.abortReshardCollection()`.
Examples
--------
Reshard a Collection
~~~~~~~~~~~~~~~~~~~~
The following example reshards the ``sales.orders`` collection with the
new shard key ``{ order_id: 1 }``:
.. code-block:: javascript
sh.reshardCollection( "sales.orders", { order_id: 1 } )
Example output:
.. code-block:: javascript
:copyable: false
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp(1, 1624887954),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: 0
}
},
operationTime: Timestamp(1, 1624887947)
}
.. _reshardCollection-to-same-key:
Reshard a Collection to the Same Shard Key
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In order to reshard to the same shard key, set :ref:`forceRedistribution
<forceRedistribution-option>` to ``true``. The following example
reshards the ``sales.orders`` collection to the same shard key
``{ order_id: 1 }`` and redistributes data.
.. code-block:: javascript
sh.reshardCollection(
"sales.orders",
{ order_id: 1 },
{ forceRedistribution: true }
)
Example output:
.. code-block:: javascript
:copyable: false
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1733502241, i: 20 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1733502241, i: 20 })
}
Reshard a Collection with Zones
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reshard a collection with zones when you need to adjust the distribution
of data across the shards in your cluster to meet changing requirements or
to improve performance.
In the following example, the ``test.scores`` collection resides on ``shard0``
and ``shard1``. The current shard key is ``{ _id: 1}``.
.. procedure::
:style: normal
.. step:: Add shards to a new zone
In this example, this zone is called ``NewZone``.
.. code-block:: javascript
sh.addShardToZone( "shard2", "NewZone" )
sh.addShardToZone( "shard3", "NewZone" )
.. step:: Run ``sh.reshardCollection`` with the new zone information
.. code-block:: javascript
sh.reshardCollection(
"test.scores",
{ "studentId": 1, "testId": 1},
{ zones: [ {
min: { "studentId": MinKey(), "testId": MinKey() },
max: { "studentId": MaxKey(), "testId": MaxKey() },
zone: "NewZone" }
]
} )
The resharding operation adds the shards in zone ``NewZone`` as recipients.
The database primary shard is added as a recipient as a backstop for any
missing ranges in the zone definition. If there are no missing ranges, the
collection is cloned on shards in the "NewZone", such as ``shard2`` and
``shard3`` in this example. ``sh.reshardCollection`` returns the following:
.. code-block:: javascript
:copyable: false
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp( { t: 1699484530, i: 54 } ),
signature: {
hash: Binary.createFromBase64( "90ApBDrSSi4XnCpV3OWIH4OGO0Y=", 0 ),
keyId: Long( "7296989036055363606" )
} },
operationTime: Timestamp( { t: 1699484530, i: 54 } )
}
Learn More
----------
- :ref:`<sharding-resharding>`