Skip to content

Commit 22cbe92

Browse files
(DOCS-16193): Mention non-guaranteed order of $accumulator (#7008) (#7016)
* (DOCS-16193): Mention non-guaranteed order of * review edits * formatting fix * wording * formatting fix * minimalism * add copyable false * wording * change heading level * nits
1 parent fabef6d commit 22cbe92

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

source/reference/operator/aggregation/accumulator.txt

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,13 @@ may need to merge two separate, intermediate states. The :ref:`merge
242242
<accumulator-merge>` function specifies how the operator should merge
243243
two states.
244244

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:
246252

247253
- :group:`$accumulator` is run on a sharded cluster. The operator
248254
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:
255261
Once the operation finishes, the results from disk and memory are
256262
merged together using the :ref:`merge <accumulator-merge>` function.
257263

258-
.. seealso::
264+
Document Processing Order
265+
~~~~~~~~~~~~~~~~~~~~~~~~~
259266

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

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``.
263319

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

269323
Javascript Enabled
270324
~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)