As per MongoDB Documentation here Groups documents by some specified expression and outputs to the next stage a document for each distinct grouping. The output documents contain an _id field which contains the distinct group by key. The output documents can also contain computed fields that hold the values of some accumulator expression grouped by the $group’s _id field. $group does not order its output documents.
The $group stage has the following prototype form:
{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }
The _id field is mandatory; however, you can specify an _id value of null, or any other constant value, to calculate accumulated values for all the input documents as a whole.
$group Operator and Memory
The $group stage has a limit of 100 megabytes of RAM. By default, if the stage exceeds this limit, $group will produce an error. However, to allow for the handling of large datasets, set the allowDiskUse option to true to enable $group operations to write to temporary files.
As per blog of @VladMihalcea here the MongoDB aggregation framework is extremely useful and its performances can’t go unnoticed.that didn’t require any extra optimization, aiming to demonstrate the out-of-the-box performance of MongoDB.
For further your ref here