PHP & MongoDB – Overview
PHP developer team has provided MongoDB Driver for PHP and have various resources available for it.
First step in connecting to MongoDB using PHP is to have mongodb PHP driver dll in php ext directory and enable it in php.ini and then use mongodb API to connect to the database.
Connecting to MongoDB database
Suppose, MongoDB is installed locally and using default port then following syntax connects to MongoDB database.
$manager = new MongoDBDriverManager("mongodb://localhost:27017");
MongoDB Driver Manager is the central class to do all the operations on mongodb database. To connect to mongodb database, we connect to database by first preparing a command and then executing it.
$statistics = new MongoDBDriverCommand(["dbstats" => 1]); $cursor = $manager->executeCommand("mydb", $statistics);
Once command is executed, if mydb database is not present, it will be created otherwise, it will be connected.