public function connect()
    {

        if ($this->_connection && $this->_connection->connect()) {
            return true;
        }
        /**
         * Add required variables
         * Clear the connection parameters for security
         */
        $config = $this->_config['connection'] + array(
                'hostnames'  => 'localhost:27017'
        );

        unset($this->_config['connection']);

        /* Add Username & Password to server string */
        if (isset($config['username']) && isset($config['password'])) {
            $config['hostnames'] = $config['username'] . ':' . $config['password'] . '@' . $config['hostnames'] . '/' . $config['database'];
        }

        /* Add required 'mongodb://' prefix */
        if (strpos($config['hostnames'], 'mongodb://') !== 0) {
            $config['hostnames'] = 'mongodb://' . $config['hostnames'];
        }

        if (!isset($config['options']) || !is_array($config['options'])) {
            $config['options'] = array();
        }

        /* Create connection object, attempt to connect */
        $config['options']['connect'] = false;

        $class = '\MongoClient';
        if (!class_exists($class)) {
            $class = '\Mongo';
        }

        $this->_connection = new $class($config['hostnames'], $config['options']);

        /* Try connect */
        try {
            $this->_connection->connect();
        } catch (\MongoConnectionException $e) {
            throw new \Exception('Unable to connect to MongoDB server at ' . $config['hostnames']);
        }

        if (!isset($config['database'])) {
            throw new \Exception('No database specified in MangoDB Config');
        }

        $this->_db = $this->_connection->selectDB($config['database']);

        if(!$this->_db instanceof \MongoDB) {
            throw new \Exception('Unable to connect to database :: $_db is ' . gettype($this->_db));
        }
        
        $this->_connected = true;

        return true;
    }
public function connect()
    {

        if ($this->_connection && $this->_connection->connect()) {
            return true;
        }
        /**
         * Add required variables
         * Clear the connection parameters for security
         */
        $config = $this->_config['connection'] + array(
                'hostnames'  => 'localhost:27017'
        );

        unset($this->_config['connection']);

        /* Add Username & Password to server string */
        if (isset($config['username']) && isset($config['password'])) {
            $config['hostnames'] = $config['username'] . ':' . $config['password'] . '@' . $config['hostnames'] . '/' . $config['database'];
        }

        /* Add required 'mongodb://' prefix */
        if (strpos($config['hostnames'], 'mongodb://') !== 0) {
            $config['hostnames'] = 'mongodb://' . $config['hostnames'];
        }

        if (!isset($config['options']) || !is_array($config['options'])) {
            $config['options'] = array();
        }

        /* Create connection object, attempt to connect */
        $config['options']['connect'] = false;

        $class = '\MongoClient';
        if (!class_exists($class)) {
            $class = '\Mongo';
        }

        $this->_connection = new $class($config['hostnames'], $config['options']);

        /* Try connect */
        try {
            $this->_connection->connect();
        } catch (\MongoConnectionException $e) {
            throw new \Exception('Unable to connect to MongoDB server at ' . $config['hostnames']);
        }

        if (!isset($config['database'])) {
            throw new \Exception('No database specified in MangoDB Config');
        }

        $this->_db = $this->_connection->selectDB($config['database']);

        if(!$this->_db instanceof \MongoDB) {
            throw new \Exception('Unable to connect to database :: $_db is ' . gettype($this->_db));
        }
        
        $this->_connected = true;

        return true;
    }
public function connect ( ) { if ( $ this -> _connection && $ this -> _connection -> connect ( ) ) { return true ; } /**
         * Add required variables
         * Clear the connection parameters for security
         */ $ config = $ this -> _config [ 'connection' ] + array ( 'hostnames' => 'localhost:27017' ) ; unset ( $ this -> _config [ 'connection' ] ) ; /* Add Username & Password to server string */ if ( isset ( $ config [ 'username' ] ) && isset ( $ config [ 'password' ] ) ) { $ config [ 'hostnames' ] = $ config [ 'username' ] . ':' . $ config [ 'password' ] . '@' . $ config [ 'hostnames' ] . '/' . $ config [ 'database' ] ; } /* Add required 'mongodb://' prefix */ if ( strpos ( $ config [ 'hostnames' ] , 'mongodb://' ) !== 0 ) { $ config [ 'hostnames' ] = 'mongodb://' . $ config [ 'hostnames' ] ; } if ( ! isset ( $ config [ 'options' ] ) || ! is_array ( $ config [ 'options' ] ) ) { $ config [ 'options' ] = array ( ) ; } /* Create connection object, attempt to connect */ $ config [ 'options' ] [ 'connect' ] = false ; $ class = '\MongoClient' ; if ( ! class_exists ( $ class ) ) { $ class = '\Mongo' ; } $ this -> _connection = new $ class ( $ config [ 'hostnames' ] , $ config [ 'options' ] ) ; /* Try connect */ try { $ this -> _connection -> connect ( ) ; } catch ( \ MongoConnectionException $ e ) { throw new \ Exception ( 'Unable to connect to MongoDB server at ' . $ config [ 'hostnames' ] ) ; } if ( ! isset ( $ config [ 'database' ] ) ) { throw new \ Exception ( 'No database specified in MangoDB Config' ) ; } $ this -> _db = $ this -> _connection -> selectDB ( $ config [ 'database' ] ) ; if ( ! $ this -> _db instanceof \ MongoDB ) { throw new \ Exception ( 'Unable to connect to database :: $_db is ' . gettype ( $ this -> _db ) ) ; } $ this -> _connected = true ; return true ; }
Connect to MongoDB, select database

@throws \Exception
@return bool
Connect to MongoDB select database