MongoClient::connect PHP Method

connect() public method

Connects to a database server or replica set
public connect ( ) : boolean
return boolean - If the connection was successful.
    public function connect()
    {
        if ($this->protocols) {
            return true;
        }
        if ($this->replSet) {
            $this->connectToReplSet();
        } else {
            $this->connectToFirstAvailableHost();
        }
        return $this->connected = true;
    }

Usage Example

示例#1
0
 public function connect()
 {
     if ($this->_connection) {
         return;
     }
     $host = $this->_config['connection']['server'];
     if (isset($this->_config['connection']['username']) && isset($this->_config['connection']['password'])) {
         $host = $this->_config['connection']['username'] . ':' . $this->_config['connection']['password'] . '@' . $host;
     }
     if (strpos($host, 'mongodb://') !== 0) {
         $host = 'mongodb://' . $host;
     }
     if (!isset($options)) {
         $options = array();
     }
     $options['connect'] = FALSE;
     $this->_connection = new MongoClient($host, $options);
     try {
         $this->_connection->connect();
     } catch (MongoConnectionException $e) {
         throw new Exception('Unable to connect to Mongo server at :hostnames', array(':hostnames' => $e->getMessage()));
     }
     if (!isset($this->_config['connection']['db'])) {
         throw new Exception('No database specified in MangoDB Config');
     }
     $this->_db = $this->_connection->selectDB($this->_config['connection']['db']);
     return $this->_connected = TRUE;
 }
All Usage Examples Of MongoClient::connect