When using an $accumulator is it safe to "modify" the existing state and return it?

Hi everyone. Thanks for the $accumulator - it’s amazing. We are building some amazing things with it, in combination with the group aggregation step. It feels like super-powers!

I was wondering, if it’s safe to do something like the following, where the state is mutated and then returned:

function accumulateFn(someState) {
  someState.fooBar = 2;
  return someState;
// vs
function accumulateFn(someState) {
  return {
    fooBar: 2

(I have some pretty complex $accumulator’s that would be very simple to maintain, if I can do something like state.path.to.some.object += 3; return state;. Thanks in advance :+1:

hi @Alex_Bjorlig, it is safe. The aggregation and Javascript use different formats internally, it’s not holding on to any Javascript values between calls (the accumulator’s internal state is a variable in C++, which has nothing to do with Javascript).


Ok - thanks for clarification :+1:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.