lib/util/mongo.js
function createDb(config, dbUser, dbUserPass, dbName, cb) {
  log.logger.trace({user: dbUser, pwd: dbUserPass, name: dbName}, 'creating new datatbase');
  var url = config.mongoUrl;
  var admin = config.mongo.admin_auth.user;
  var admin_pass = config.mongo.admin_auth.pass;

  MongoClient.connect(url, function(err, db){
    if (err) return handleError(null, err, 'cannot open mongodb connection', cb);

    var targetDb = db.db(dbName);
    targetDb.authenticate(admin, admin_pass, {'authSource': 'admin'}, function(err) {
      if (err) return handleError(db, err, 'can not authenticate admin user', cb);

      // update for mongodb3.2 - need to remove user first
      targetDb.removeUser(dbUser, function(err) {
        if(err){
          log.logger.error(err, 'failed to remove user');
        }
        // add user to database
        targetDb.addUser(dbUser, dbUserPass, function(err, user) {
          if (err) return handleError(db, err, 'can not add user', cb);
          log.logger.trace({user: user, database: dbName}, 'mongo added new user');
          db.close();
          return cb();
        });
      });
    });
  });
}
function createDb(config, dbUser, dbUserPass, dbName, cb) {
  log.logger.trace({user: dbUser, pwd: dbUserPass, name: dbName}, 'creating new datatbase');
  var url = config.mongoUrl;
  var admin = config.mongo.admin_auth.user;
  var admin_pass = config.mongo.admin_auth.pass;

  MongoClient.connect(url, function(err, db){
    if (err) return handleError(null, err, 'cannot open mongodb connection', cb);

    var targetDb = db.db(dbName);
    targetDb.authenticate(admin, admin_pass, {'authSource': 'admin'}, function(err) {
      if (err) return handleError(db, err, 'can not authenticate admin user', cb);

      // update for mongodb3.2 - need to remove user first
      targetDb.removeUser(dbUser, function(err) {
        if(err){
          log.logger.error(err, 'failed to remove user');
        }
        // add user to database
        targetDb.addUser(dbUser, dbUserPass, function(err, user) {
          if (err) return handleError(db, err, 'can not add user', cb);
          log.logger.trace({user: user, database: dbName}, 'mongo added new user');
          db.close();
          return cb();
        });
      });
    });
  });
}
function createDb ( config , dbUser , dbUserPass , dbName , cb ) { log . logger . trace ( { user : dbUser , pwd : dbUserPass , name : dbName } , 'creating new datatbase' ) ; var url = config . mongoUrl ; var admin = config . mongo . admin_auth . user ; var admin_pass = config . mongo . admin_auth . pass ; MongoClient . connect ( url , function ( err , db ) { if ( err ) return handleError ( null , err , 'cannot open mongodb connection' , cb ) ; var targetDb = db . db ( dbName ) ; targetDb . authenticate ( admin , admin_pass , { 'authSource' : 'admin' } , function ( err ) { if ( err ) return handleError ( db , err , 'can not authenticate admin user' , cb ) ; // update for mongodb3.2 - need to remove user first targetDb . removeUser ( dbUser , function ( err ) { if ( err ) { log . logger . error ( err , 'failed to remove user' ) ; } // add user to database targetDb . addUser ( dbUser , dbUserPass , function ( err , user ) { if ( err ) return handleError ( db , err , 'can not add user' , cb ) ; log . logger . trace ( { user : user , database : dbName } , 'mongo added new user' ) ; db . close ( ) ; return cb ( ) ; } ) ; } ) ; } ) ; } ) ; }
create a database, including user name and pwd
create a database including user name and pwd