lib/mongodb/mongodb/lib/operations/db_ops.js
function addUser(db, username, password, options, callback) {
  const Db = require('../db');

  // Did the user destroy the topology
  if (db.serverConfig && db.serverConfig.isDestroyed())
    return callback(new MongoError('topology was destroyed'));
  // Attempt to execute auth command
  executeAuthCreateUserCommand(db, username, password, options, (err, r) => {
    // We need to perform the backward compatible insert operation
    if (err && err.code === -5000) {
      const finalOptions = applyWriteConcern(Object.assign({}, options), { db }, options);

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

      // If we have another db set
      const db = options.dbName ? new Db(options.dbName, db.s.topology, db.s.options) : db;

      // Fetch a user collection
      const collection = db.collection(CONSTANTS.SYSTEM_USER_COLLECTION);

      // Check if we are inserting the first user
      count(collection, {}, finalOptions, (err, count) => {
        // We got an error (f.ex not authorized)
        if (err != null) return handleCallback(callback, err, null);
        // Check if the user exists and update i
        const findOptions = Object.assign({ projection: { dbName: 1 } }, finalOptions);
        collection.find({ user: username }, findOptions).toArray(err => {
          // We got an error (f.ex not authorized)
          if (err != null) return handleCallback(callback, err, null);
          // Add command keys
          finalOptions.upsert = true;

          // We have a user, let's update the password or upsert if not
          updateOne(
            collection,
            { user: username },
            { $set: { user: username, pwd: userPassword } },
            finalOptions,
            err => {
              if (count === 0 && err)
                return handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
              if (err) return handleCallback(callback, err, null);
              handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
            }
          );
        });
      });

      return;
    }

    if (err) return handleCallback(callback, err);
    handleCallback(callback, err, r);
  });
}
function addUser(db, username, password, options, callback) {
  const Db = require('../db');

  // Did the user destroy the topology
  if (db.serverConfig && db.serverConfig.isDestroyed())
    return callback(new MongoError('topology was destroyed'));
  // Attempt to execute auth command
  executeAuthCreateUserCommand(db, username, password, options, (err, r) => {
    // We need to perform the backward compatible insert operation
    if (err && err.code === -5000) {
      const finalOptions = applyWriteConcern(Object.assign({}, options), { db }, options);

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

      // If we have another db set
      const db = options.dbName ? new Db(options.dbName, db.s.topology, db.s.options) : db;

      // Fetch a user collection
      const collection = db.collection(CONSTANTS.SYSTEM_USER_COLLECTION);

      // Check if we are inserting the first user
      count(collection, {}, finalOptions, (err, count) => {
        // We got an error (f.ex not authorized)
        if (err != null) return handleCallback(callback, err, null);
        // Check if the user exists and update i
        const findOptions = Object.assign({ projection: { dbName: 1 } }, finalOptions);
        collection.find({ user: username }, findOptions).toArray(err => {
          // We got an error (f.ex not authorized)
          if (err != null) return handleCallback(callback, err, null);
          // Add command keys
          finalOptions.upsert = true;

          // We have a user, let's update the password or upsert if not
          updateOne(
            collection,
            { user: username },
            { $set: { user: username, pwd: userPassword } },
            finalOptions,
            err => {
              if (count === 0 && err)
                return handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
              if (err) return handleCallback(callback, err, null);
              handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
            }
          );
        });
      });

      return;
    }

    if (err) return handleCallback(callback, err);
    handleCallback(callback, err, r);
  });
}
function addUser ( db , username , password , options , callback ) { const Db = require ( '../db' ) ; // Did the user destroy the topology if ( db . serverConfig && db . serverConfig . isDestroyed ( ) ) return callback ( new MongoError ( 'topology was destroyed' ) ) ; // Attempt to execute auth command executeAuthCreateUserCommand ( db , username , password , options , ( err , r ) => { // We need to perform the backward compatible insert operation if ( err && err . code === - 5000 ) { const finalOptions = applyWriteConcern ( Object . assign ( { } , options ) , { db } , options ) ; // Use node md5 generator const md5 = crypto . createHash ( 'md5' ) ; // Generate keys used for authentication md5 . update ( username + ':mongo:' + password ) ; const userPassword = md5 . digest ( 'hex' ) ; // If we have another db set const db = options . dbName ? new Db ( options . dbName , db . s . topology , db . s . options ) : db ; // Fetch a user collection const collection = db . collection ( CONSTANTS . SYSTEM_USER_COLLECTION ) ; // Check if we are inserting the first user count ( collection , { } , finalOptions , ( err , count ) => { // We got an error (f.ex not authorized) if ( err != null ) return handleCallback ( callback , err , null ) ; // Check if the user exists and update i const findOptions = Object . assign ( { projection : { dbName : 1 } } , finalOptions ) ; collection . find ( { user : username } , findOptions ) . toArray ( err => { // We got an error (f.ex not authorized) if ( err != null ) return handleCallback ( callback , err , null ) ; // Add command keys finalOptions . upsert = true ; // We have a user, let's update the password or upsert if not updateOne ( collection , { user : username } , { $set : { user : username , pwd : userPassword } } , finalOptions , err => { if ( count === 0 && err ) return handleCallback ( callback , null , [ { user : username , pwd : userPassword } ] ) ; if ( err ) return handleCallback ( callback , err , null ) ; handleCallback ( callback , null , [ { user : username , pwd : userPassword } ] ) ; } ) ; } ) ; } ) ; return ; } if ( err ) return handleCallback ( callback , err ) ; handleCallback ( callback , err , r ) ; } ) ; }
Add a user to the database.
@method
@param {Db} db The Db instance on which to add a user.
@param {string} username The username.
@param {string} password The password.
@param {object} [options] Optional settings. See Db.prototype.addUser for a list of options.
@param {Db~resultCallback} [callback] The command result callback
Add a user to the database .