lib/operations/db_ops.js
function executeAuthCreateUserCommand(db, username, password, options, callback) {
  // Special case where there is no password ($external users)
  if (typeof username === 'string' && password != null && typeof password === 'object') {
    options = password;
    password = null;
  }

  // Unpack all options
  if (typeof options === 'function') {
    callback = options;
    options = {};
  }

  // Error out if we digestPassword set
  if (options.digestPassword != null) {
    return callback(
      toError(
        "The digestPassword option is not supported via add_user. Please use db.command('createUser', ...) instead for this option."
      )
    );
  }

  // Get additional values
  const customData = options.customData != null ? options.customData : {};
  let roles = Array.isArray(options.roles) ? options.roles : [];
  const maxTimeMS = typeof options.maxTimeMS === 'number' ? options.maxTimeMS : null;

  // If not roles defined print deprecated message
  if (roles.length === 0) {
    console.log('Creating a user without roles is deprecated in MongoDB >= 2.6');
  }

  // Get the error options
  const commandOptions = { writeCommand: true };
  if (options['dbName']) commandOptions.dbName = options['dbName'];

  // Add maxTimeMS to options if set
  if (maxTimeMS != null) commandOptions.maxTimeMS = maxTimeMS;

  // Check the db name and add roles if needed
  if (
    (db.databaseName.toLowerCase() === 'admin' || options.dbName === 'admin') &&
    !Array.isArray(options.roles)
  ) {
    roles = ['root'];
  } else if (!Array.isArray(options.roles)) {
    roles = ['dbOwner'];
  }

  const digestPassword = db.s.topology.lastIsMaster().maxWireVersion >= 7;

  // Build the command to execute
  let command = {
    createUser: username,
    customData: customData,
    roles: roles,
    digestPassword
  };

  // Apply write concern to command
  command = applyWriteConcern(command, { db }, options);

  let userPassword = password;

  if (!digestPassword) {
    // Use node md5 generator
    const md5 = crypto.createHash('md5');
    // Generate keys used for authentication
    md5.update(username + ':mongo:' + password);
    userPassword = md5.digest('hex');
  }

  // No password
  if (typeof password === 'string') {
    command.pwd = userPassword;
  }

  // Force write using primary
  commandOptions.readPreference = ReadPreference.primary;

  // Execute the command
  executeCommand(db, command, commandOptions, (err, result) => {
    if (err && err.ok === 0 && err.code === undefined)
      return handleCallback(callback, { code: -5000 }, null);
    if (err) return handleCallback(callback, err, null);
    handleCallback(
      callback,
      !result.ok ? toError(result) : null,
      result.ok ? [{ user: username, pwd: '' }] : null
    );
  });
}
function executeAuthCreateUserCommand(db, username, password, options, callback) {
  // Special case where there is no password ($external users)
  if (typeof username === 'string' && password != null && typeof password === 'object') {
    options = password;
    password = null;
  }

  // Unpack all options
  if (typeof options === 'function') {
    callback = options;
    options = {};
  }

  // Error out if we digestPassword set
  if (options.digestPassword != null) {
    return callback(
      toError(
        "The digestPassword option is not supported via add_user. Please use db.command('createUser', ...) instead for this option."
      )
    );
  }

  // Get additional values
  const customData = options.customData != null ? options.customData : {};
  let roles = Array.isArray(options.roles) ? options.roles : [];
  const maxTimeMS = typeof options.maxTimeMS === 'number' ? options.maxTimeMS : null;

  // If not roles defined print deprecated message
  if (roles.length === 0) {
    console.log('Creating a user without roles is deprecated in MongoDB >= 2.6');
  }

  // Get the error options
  const commandOptions = { writeCommand: true };
  if (options['dbName']) commandOptions.dbName = options['dbName'];

  // Add maxTimeMS to options if set
  if (maxTimeMS != null) commandOptions.maxTimeMS = maxTimeMS;

  // Check the db name and add roles if needed
  if (
    (db.databaseName.toLowerCase() === 'admin' || options.dbName === 'admin') &&
    !Array.isArray(options.roles)
  ) {
    roles = ['root'];
  } else if (!Array.isArray(options.roles)) {
    roles = ['dbOwner'];
  }

  const digestPassword = db.s.topology.lastIsMaster().maxWireVersion >= 7;

  // Build the command to execute
  let command = {
    createUser: username,
    customData: customData,
    roles: roles,
    digestPassword
  };

  // Apply write concern to command
  command = applyWriteConcern(command, { db }, options);

  let userPassword = password;

  if (!digestPassword) {
    // Use node md5 generator
    const md5 = crypto.createHash('md5');
    // Generate keys used for authentication
    md5.update(username + ':mongo:' + password);
    userPassword = md5.digest('hex');
  }

  // No password
  if (typeof password === 'string') {
    command.pwd = userPassword;
  }

  // Force write using primary
  commandOptions.readPreference = ReadPreference.primary;

  // Execute the command
  executeCommand(db, command, commandOptions, (err, result) => {
    if (err && err.ok === 0 && err.code === undefined)
      return handleCallback(callback, { code: -5000 }, null);
    if (err) return handleCallback(callback, err, null);
    handleCallback(
      callback,
      !result.ok ? toError(result) : null,
      result.ok ? [{ user: username, pwd: '' }] : null
    );
  });
}
function executeAuthCreateUserCommand ( db , username , password , options , callback ) { // Special case where there is no password ($external users) if ( typeof username === 'string' && password != null && typeof password === 'object' ) { options = password ; password = null ; } // Unpack all options if ( typeof options === 'function' ) { callback = options ; options = { } ; } // Error out if we digestPassword set if ( options . digestPassword != null ) { return callback ( toError ( "The digestPassword option is not supported via add_user. Please use db.command('createUser', ...) instead for this option." ) ) ; } // Get additional values const customData = options . customData != null ? options . customData : { } ; let roles = Array . isArray ( options . roles ) ? options . roles : [ ] ; const maxTimeMS = typeof options . maxTimeMS === 'number' ? options . maxTimeMS : null ; // If not roles defined print deprecated message if ( roles . length === 0 ) { console . log ( 'Creating a user without roles is deprecated in MongoDB >= 2.6' ) ; } // Get the error options const commandOptions = { writeCommand : true } ; if ( options [ 'dbName' ] ) commandOptions . dbName = options [ 'dbName' ] ; // Add maxTimeMS to options if set if ( maxTimeMS != null ) commandOptions . maxTimeMS = maxTimeMS ; // Check the db name and add roles if needed if ( ( db . databaseName . toLowerCase ( ) === 'admin' || options . dbName === 'admin' ) && ! Array . isArray ( options . roles ) ) { roles = [ 'root' ] ; } else if ( ! Array . isArray ( options . roles ) ) { roles = [ 'dbOwner' ] ; } const digestPassword = db . s . topology . lastIsMaster ( ) . maxWireVersion >= 7 ; // Build the command to execute let command = { createUser : username , customData : customData , roles : roles , digestPassword } ; // Apply write concern to command command = applyWriteConcern ( command , { db } , options ) ; let userPassword = password ; if ( ! digestPassword ) { // Use node md5 generator const md5 = crypto . createHash ( 'md5' ) ; // Generate keys used for authentication md5 . update ( username + ':mongo:' + password ) ; userPassword = md5 . digest ( 'hex' ) ; } // No password if ( typeof password === 'string' ) { command . pwd = userPassword ; } // Force write using primary commandOptions . readPreference = ReadPreference . primary ; // Execute the command executeCommand ( db , command , commandOptions , ( err , result ) => { if ( err && err . ok === 0 && err . code === undefined ) return handleCallback ( callback , { code : - 5000 } , null ) ; if ( err ) return handleCallback ( callback , err , null ) ; handleCallback ( callback , ! result . ok ? toError ( result ) : null , result . ok ? [ { user : username , pwd : '' } ] : null ) ; } ) ; }
Run the createUser command.

@param {Db} db The Db instance on which to execute the command.
@param {string} username The username of the user to add.
@param {string} password The password of the user to add.
@param {object} [options] Optional settings. See Db.prototype.addUser for a list of options.
@param {Db~resultCallback} [callback] The command result callback
Run the createUser command .