@@ -242,7 +242,13 @@ may need to merge two separate, intermediate states. The :ref:`merge
242
242
<accumulator-merge>` function specifies how the operator should merge
243
243
two states.
244
244
245
- For example, :group:`$accumulator` may need to combine two states when:
245
+ The :ref:`merge <accumulator-merge>` function always merges two
246
+ states at a time. In the event that more than two states must be merged,
247
+ the resulting merge of two states is merged with a single state. This
248
+ process repeats until all states are merged.
249
+
250
+ For example, :group:`$accumulator` may need to combine two states in the
251
+ following scenarios:
246
252
247
253
- :group:`$accumulator` is run on a sharded cluster. The operator
248
254
needs to merge the results from each shard to obtain the final
@@ -255,16 +261,64 @@ For example, :group:`$accumulator` may need to combine two states when:
255
261
Once the operation finishes, the results from disk and memory are
256
262
merged together using the :ref:`merge <accumulator-merge>` function.
257
263
258
- .. seealso::
264
+ Document Processing Order
265
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
259
266
260
- :ref:`group-memory-limit`
267
+ The order that MongoDB processes documents for the ``init()``,
268
+ ``accumulate()``, and ``merge()`` functions can vary, and might differ
269
+ from the order that those documents are specified to the
270
+ ``$accumulator`` function.
261
271
262
- .. note::
272
+ For example, consider a series of documents where the ``_id`` fields are
273
+ the letters of the alphabet:
274
+
275
+ .. code-block:: javascript
276
+ :copyable: false
277
+
278
+ { _id: 'a' },
279
+ { _id: 'b' },
280
+ { _id: 'c' }
281
+ ...
282
+ { _id: 'z' }
283
+
284
+ Next, consider an aggregation pipeline that sorts the documents by the
285
+ ``_id`` field and then uses an ``$accumulator`` function to concatenate
286
+ the ``_id`` field values:
287
+
288
+ .. code-block:: javascript
289
+
290
+ [
291
+ {
292
+ $sort: { _id: 1 }
293
+ },
294
+ {
295
+ $group: {
296
+ _id: null,
297
+ alphabet: {
298
+ $accumulator: {
299
+ init: function() {
300
+ return ""
301
+ },
302
+ accumulate: function(state, letter) {
303
+ return(state + letter)
304
+ },
305
+ accumulateArgs: [ "$_id" ],
306
+ merge: function(state1, state2) {
307
+ return(state1 + state2)
308
+ },
309
+ lang: "js"
310
+ }
311
+ }
312
+ }
313
+ }
314
+ ]
315
+
316
+ MongoDB does not guarantee that the documents are processed in the
317
+ sorted order, meaning the ``alphabet`` field does not necessarily get
318
+ set to ``abc...z``.
263
319
264
- The :ref:`merge <accumulator-merge>` function always merges two
265
- states at a time. In the event that more than two states must be
266
- merged, the resulting merge of two states is merged with a single
267
- state. This process repeats until all states are merged.
320
+ Due to this behavior, ensure that your ``$accumulator`` function does
321
+ not need to process and return documents in a specific order.
268
322
269
323
Javascript Enabled
270
324
~~~~~~~~~~~~~~~~~~
0 commit comments