Hello, I have an urgent problem regarding to the Mongo client connections, I want to understand why is creating a new connection almost all the time. I’ve tried to handling different connections in an array but I still create too many connections. I’m using PHP 5.6 with Mongo 3.6.17 and the mongo php driver is GitHub - mongodb/mongo-php-driver-legacy: Legacy MongoDB PHP driver.
This is the Connect function:
function Connect ($dbName, $dbURI){
if (!empty($this->connection)) {
return; // Avoiding to instantiate a new MongoClient.
} else {
$options = [
'connect' => true,
'connectTimeoutMS' => 10000,
];
$mongo = new MongoClient(
$dbURI,
$options
);
if ( count( explode( ',', $dbURI ) ) > 1 ) {
$mongo -> setReadPreference( MongoClient::RP_SECONDARY_PREFERRED );
$this -> replica = true;
$mongo -> slaveOkay = true;
}
$this -> connection = $mongo -> $dbName;
As example I have: a Function with $mongoConn = new MyClassMongo($db); after I just use the Find method or any other method to operate with mongo.
Thank you.