lib/operations/cursor_ops.js
function count(cursor, applySkipLimit, opts, callback) {
  if (applySkipLimit) {
    if (typeof cursor.cursorSkip() === 'number') opts.skip = cursor.cursorSkip();
    if (typeof cursor.cursorLimit() === 'number') opts.limit = cursor.cursorLimit();
  }

  // Ensure we have the right read preference inheritance
  if (opts.readPreference) {
    cursor.setReadPreference(opts.readPreference);
  }

  if (
    typeof opts.maxTimeMS !== 'number' &&
    cursor.s.cmd &&
    typeof cursor.s.cmd.maxTimeMS === 'number'
  ) {
    opts.maxTimeMS = cursor.s.cmd.maxTimeMS;
  }

  let options = {};
  options.skip = opts.skip;
  options.limit = opts.limit;
  options.hint = opts.hint;
  options.maxTimeMS = opts.maxTimeMS;

  // Command
  const delimiter = cursor.s.ns.indexOf('.');
  options.collectionName = cursor.s.ns.substr(delimiter + 1);

  let command;
  try {
    command = buildCountCommand(cursor, cursor.s.cmd.query, options);
  } catch (err) {
    return callback(err);
  }

  // Set cursor server to the same as the topology
  cursor.server = cursor.topology.s.coreTopology;

  // Execute the command
  cursor.s.topology.command(
    `${cursor.s.ns.substr(0, delimiter)}.$cmd`,
    command,
    cursor.s.options,
    (err, result) => {
      callback(err, result ? result.result.n : null);
    }
  );
}
function count(cursor, applySkipLimit, opts, callback) {
  if (applySkipLimit) {
    if (typeof cursor.cursorSkip() === 'number') opts.skip = cursor.cursorSkip();
    if (typeof cursor.cursorLimit() === 'number') opts.limit = cursor.cursorLimit();
  }

  // Ensure we have the right read preference inheritance
  if (opts.readPreference) {
    cursor.setReadPreference(opts.readPreference);
  }

  if (
    typeof opts.maxTimeMS !== 'number' &&
    cursor.s.cmd &&
    typeof cursor.s.cmd.maxTimeMS === 'number'
  ) {
    opts.maxTimeMS = cursor.s.cmd.maxTimeMS;
  }

  let options = {};
  options.skip = opts.skip;
  options.limit = opts.limit;
  options.hint = opts.hint;
  options.maxTimeMS = opts.maxTimeMS;

  // Command
  const delimiter = cursor.s.ns.indexOf('.');
  options.collectionName = cursor.s.ns.substr(delimiter + 1);

  let command;
  try {
    command = buildCountCommand(cursor, cursor.s.cmd.query, options);
  } catch (err) {
    return callback(err);
  }

  // Set cursor server to the same as the topology
  cursor.server = cursor.topology.s.coreTopology;

  // Execute the command
  cursor.s.topology.command(
    `${cursor.s.ns.substr(0, delimiter)}.$cmd`,
    command,
    cursor.s.options,
    (err, result) => {
      callback(err, result ? result.result.n : null);
    }
  );
}
function count ( cursor , applySkipLimit , opts , callback ) { if ( applySkipLimit ) { if ( typeof cursor . cursorSkip ( ) === 'number' ) opts . skip = cursor . cursorSkip ( ) ; if ( typeof cursor . cursorLimit ( ) === 'number' ) opts . limit = cursor . cursorLimit ( ) ; } // Ensure we have the right read preference inheritance if ( opts . readPreference ) { cursor . setReadPreference ( opts . readPreference ) ; } if ( typeof opts . maxTimeMS !== 'number' && cursor . s . cmd && typeof cursor . s . cmd . maxTimeMS === 'number' ) { opts . maxTimeMS = cursor . s . cmd . maxTimeMS ; } let options = { } ; options . skip = opts . skip ; options . limit = opts . limit ; options . hint = opts . hint ; options . maxTimeMS = opts . maxTimeMS ; // Command const delimiter = cursor . s . ns . indexOf ( '.' ) ; options . collectionName = cursor . s . ns . substr ( delimiter + 1 ) ; let command ; try { command = buildCountCommand ( cursor , cursor . s . cmd . query , options ) ; } catch ( err ) { return callback ( err ) ; } // Set cursor server to the same as the topology cursor . server = cursor . topology . s . coreTopology ; // Execute the command cursor . s . topology . command ( ` ${ cursor . s . ns . substr ( 0 , delimiter ) } ` , command , cursor . s . options , ( err , result ) => { callback ( err , result ? result . result . n : null ) ; } ) ; }
Get the count of documents for this cursor.

@method
@param {Cursor} cursor The Cursor instance on which to count.
@param {boolean} [applySkipLimit=true] Specifies whether the count command apply limit and skip settings should be applied on the cursor or in the provided options.
@param {object} [options] Optional settings. See Cursor.prototype.count for a list of options.
@param {Cursor~countResultCallback} [callback] The result callback.
Get the count of documents for this cursor .