To connect MongoDB with PHP you need to install MongoDB php driver. After you successful install MongoDB php driver, you need to create connection with MongoDB.
MongoDB default port is 27017. To connect with localhost and default port
$connection = new MongoClient();
If you want to connect other than localhost with default port
$connection = new MongoClient( "mongodb://abcd.com" );
If you want to connect to host at a different port
$connection = new MongoClient( "mongodb://abcd.com:53554" );
Above will create successful connection with MongoDB.
If you want to select database then use the following code.
$db = $connection->databasename;
If you want to create collection then use the following code.
$collection = $db->createCollection("students");
Hope above is help you to connect MongoDB with PHP.
0 Comment(s)