lithium\data\source\MongoDb::connect()

public method

Connects to the Mongo server. Matches up parameters from the constructor to create a Mongo database connection.

Returns

boolean

Returns true the connection attempt was successful, otherwise false.

Source

	public function connect() {
		$server = $this->_classes['server'];

		if ($this->server && $this->server->getConnections() && $this->connection) {
			return $this->_isConnected = true;
		}
		$this->_isConnected = false;

		$options = [
			'connect' => true,
			'connectTimeoutMS' => $this->_config['timeout'],
			'replicaSet' => $this->_config['replicaSet'],
		];

		try {
			$this->server = new $server($this->_config['dsn'], $options);

			if ($prefs = $this->_config['readPreference']) {
				$prefs = !is_array($prefs) ? [$prefs, []] : $prefs;
				$this->server->setReadPreference($prefs[0], $prefs[1]);
			}

			if ($this->connection = $this->server->{$this->_config['database']}) {
				$this->_isConnected = true;
			}
		} catch (Exception $e) {
			throw new NetworkException("Could not connect to the database.", 503, $e);
		}
		return $this->_isConnected;
	}