Merge Aggregation error with AddFields

I’m trying to perform addFields and merge to take an entire collection every month and update it with new calculated fields. I’m getting the below error:

Here’s the code:

const agg = [
  {$match: {}},
  {$addFields: {
      avgGrowth: {
        $ceil: {
          $avg: '$history.growth'
    $merge: {
      into: { db: "mydb", coll: "test" },
      on: '_id',
      whenMatched: 'replace'

Hi @lmb,

I believe the issue is that with your version (4.2) you cannot do a merge back to the aggregate collection.

It is allowed only from 4.4