How to add an int to a $count and display it?

Hey, I have a field called names that I’m counting, I have 20,000 values.

When I display the count it say 20,000. No problem.

However, I’m trying to add the number 40,000 to that count in order to have: 40,000 + count($names) displayed.

How can I do that in the calculated fields section? Nothing works.

I tried {$add: [ {$count: [ "$NAMES" ]}, 40000]}

Hi @Lior_S -

You can’t use calculated fields for this, since a calculated field only acts on a single document at a time. To accomplish what you’re after, you’ll need to use the query bar to pre-group the data (i.e. to count it), after which you can apply further transformations. Here’s one way to do it:

[
  { 
    $group: {
      _id: {},
      rawCount: { $sum: 1 }
    }
  },
  { 
    $set: { 
      modifiedCount: { $sum: [ "$rawCount", 20000 ]}
    }
  } 
]

1 Like