In the last blog post, we have explained how Windows configures the MongoDB database, so let's now implement how to connect to the database.
Use one of the methods listed below to connect:
<?php
$connection = new Mongo ();//Connect to localhost:27017
$connection = new Mongo ("MONGODB://IP");//Connect to remote server (using default port: 27017)
$connection = new Mongo ("mongodb://ip:65432");//link to remote server, use custom port
$connection = new Mongoclie NT (); Connect to localhost:27017
$connection = new Mongoclient ("MONGODB://IP");//Connect to remote server (using default port: 27017)
$connection = New Mongoclient ("mongodb://ip:65432"); Link to a remote server, using a custom port
?>
There is no need to disconnect from the server. This driver uses a persistent connection and reuses it the next time you try to connect to the same server.
If you want to disconnect, use the following:
$connection->close ();