def user_grant_roles(name, roles, database, user=None, password=None, host=None, port=None, authdb=None):
 conn = _connect(user, password, host, port, authdb=authdb)
 if (not conn):
  return 'Failed to connect to mongo database'
 try:
  roles = _to_dict(roles)
 except Exception:
  return 'Roles provided in wrong format'
 try:
  log.info('Granting roles {0} to user {1}'.format(roles, name))
  mdb = pymongo.database.Database(conn, database)
  mdb.eval("db.grantRolesToUser('{0}', {1})".format(name, roles))
 except pymongo.errors.PyMongoError as err:
  log.error('Granting roles {0} to user {1} failed with error: {2}'.format(roles, name, str(err)))
  return str(err)
 return True

def user_grant_roles ( name , roles , database , user = None , password = None , host = None , port = None , authdb = None ) : conn = _connect ( user , password , host , port , authdb = authdb ) if ( not conn ) : return 'Failed to connect to mongo database' try : roles = _to_dict ( roles ) except Exception : return 'Roles provided in wrong format' try : log . info ( 'Granting roles {0} to user {1}' . format ( roles , name ) ) mdb = pymongo . database . Database ( conn , database ) mdb . eval ( "db.grantRolesToUser('{0}', {1})" . format ( name , roles ) ) except pymongo . errors . PyMongoError as err : log . error ( 'Granting roles {0} to user {1} failed with error: {2}' . format ( roles , name , str ( err ) ) ) return str ( err ) return True
grant one or many roles to a mongodb user cli examples: .