lib/operations/collection_ops.js
function group(coll, keys, condition, initial, reduce, finalize, command, options, callback) {
  // Execute using the command
  if (command) {
    const reduceFunction = reduce && reduce._bsontype === 'Code' ? reduce : new Code(reduce);

    const selector = {
      group: {
        ns: coll.s.name,
        $reduce: reduceFunction,
        cond: condition,
        initial: initial,
        out: 'inline'
      }
    };

    // if finalize is defined
    if (finalize != null) selector.group['finalize'] = finalize;
    // Set up group selector
    if ('function' === typeof keys || (keys && keys._bsontype === 'Code')) {
      selector.group.$keyf = keys && keys._bsontype === 'Code' ? keys : new Code(keys);
    } else {
      const hash = {};
      keys.forEach(key => {
        hash[key] = 1;
      });
      selector.group.key = hash;
    }

    options = Object.assign({}, options);
    // Ensure we have the right read preference inheritance
    options.readPreference = resolveReadPreference(options, { db: coll.s.db, collection: coll });

    // Do we have a readConcern specified
    decorateWithReadConcern(selector, coll, options);

    // Have we specified collation
    try {
      decorateWithCollation(selector, coll, options);
    } catch (err) {
      return callback(err, null);
    }

    // Execute command
    executeCommand(coll.s.db, selector, options, (err, result) => {
      if (err) return handleCallback(callback, err, null);
      handleCallback(callback, null, result.retval);
    });
  } else {
    // Create execution scope
    const scope = reduce != null && reduce._bsontype === 'Code' ? reduce.scope : {};

    scope.ns = coll.s.name;
    scope.keys = keys;
    scope.condition = condition;
    scope.initial = initial;

    // Pass in the function text to execute within mongodb.
    const groupfn = groupFunction.replace(/ reduce;/, reduce.toString() + ';');

    evaluate(coll.s.db, new Code(groupfn, scope), null, options, (err, results) => {
      if (err) return handleCallback(callback, err, null);
      handleCallback(callback, null, results.result || results);
    });
  }
}
function group(coll, keys, condition, initial, reduce, finalize, command, options, callback) {
  // Execute using the command
  if (command) {
    const reduceFunction = reduce && reduce._bsontype === 'Code' ? reduce : new Code(reduce);

    const selector = {
      group: {
        ns: coll.s.name,
        $reduce: reduceFunction,
        cond: condition,
        initial: initial,
        out: 'inline'
      }
    };

    // if finalize is defined
    if (finalize != null) selector.group['finalize'] = finalize;
    // Set up group selector
    if ('function' === typeof keys || (keys && keys._bsontype === 'Code')) {
      selector.group.$keyf = keys && keys._bsontype === 'Code' ? keys : new Code(keys);
    } else {
      const hash = {};
      keys.forEach(key => {
        hash[key] = 1;
      });
      selector.group.key = hash;
    }

    options = Object.assign({}, options);
    // Ensure we have the right read preference inheritance
    options.readPreference = resolveReadPreference(options, { db: coll.s.db, collection: coll });

    // Do we have a readConcern specified
    decorateWithReadConcern(selector, coll, options);

    // Have we specified collation
    try {
      decorateWithCollation(selector, coll, options);
    } catch (err) {
      return callback(err, null);
    }

    // Execute command
    executeCommand(coll.s.db, selector, options, (err, result) => {
      if (err) return handleCallback(callback, err, null);
      handleCallback(callback, null, result.retval);
    });
  } else {
    // Create execution scope
    const scope = reduce != null && reduce._bsontype === 'Code' ? reduce.scope : {};

    scope.ns = coll.s.name;
    scope.keys = keys;
    scope.condition = condition;
    scope.initial = initial;

    // Pass in the function text to execute within mongodb.
    const groupfn = groupFunction.replace(/ reduce;/, reduce.toString() + ';');

    evaluate(coll.s.db, new Code(groupfn, scope), null, options, (err, results) => {
      if (err) return handleCallback(callback, err, null);
      handleCallback(callback, null, results.result || results);
    });
  }
}
function group ( coll , keys , condition , initial , reduce , finalize , command , options , callback ) { // Execute using the command if ( command ) { const reduceFunction = reduce && reduce . _bsontype === 'Code' ? reduce : new Code ( reduce ) ; const selector = { group : { ns : coll . s . name , $reduce : reduceFunction , cond : condition , initial : initial , out : 'inline' } } ; // if finalize is defined if ( finalize != null ) selector . group [ 'finalize' ] = finalize ; // Set up group selector if ( 'function' === typeof keys || ( keys && keys . _bsontype === 'Code' ) ) { selector . group . $keyf = keys && keys . _bsontype === 'Code' ? keys : new Code ( keys ) ; } else { const hash = { } ; keys . forEach ( key => { hash [ key ] = 1 ; } ) ; selector . group . key = hash ; } options = Object . assign ( { } , options ) ; // Ensure we have the right read preference inheritance options . readPreference = resolveReadPreference ( options , { db : coll . s . db , collection : coll } ) ; // Do we have a readConcern specified decorateWithReadConcern ( selector , coll , options ) ; // Have we specified collation try { decorateWithCollation ( selector , coll , options ) ; } catch ( err ) { return callback ( err , null ) ; } // Execute command executeCommand ( coll . s . db , selector , options , ( err , result ) => { if ( err ) return handleCallback ( callback , err , null ) ; handleCallback ( callback , null , result . retval ) ; } ) ; } else { // Create execution scope const scope = reduce != null && reduce . _bsontype === 'Code' ? reduce . scope : { } ; scope . ns = coll . s . name ; scope . keys = keys ; scope . condition = condition ; scope . initial = initial ; // Pass in the function text to execute within mongodb. const groupfn = groupFunction . replace ( /  reduce; / , reduce . toString ( ) + ';' ) ; evaluate ( coll . s . db , new Code ( groupfn , scope ) , null , options , ( err , results ) => { if ( err ) return handleCallback ( callback , err , null ) ; handleCallback ( callback , null , results . result || results ) ; } ) ; } }
Run a group command across a collection.

@method
@param {Collection} a Collection instance.
@param {(object|array|function|code)} keys An object, array or function expressing the keys to group by.
@param {object} condition An optional condition that must be true for a row to be considered.
@param {object} initial Initial value of the aggregation counter object.
@param {(function|Code)} reduce The reduce function aggregates (reduces) the objects iterated
@param {(function|Code)} finalize An optional function to be run on each item in the result set just before the item is returned.
@param {boolean} command Specify if you wish to run using the internal group command or using eval, default is true.
@param {object} [options] Optional settings. See Collection.prototype.group for a list of options.
@param {Collection~resultCallback} [callback] The command result callback
@deprecated MongoDB 3.6 or higher will no longer support the group command. We recommend rewriting using the aggregation framework.
Run a group command across a collection .