- PHP & MongoDB Tutorial
- PHP & MongoDB - Home
- PHP & MongoDB - Overview
- PHP & MongoDB - Environment Setup
- PHP & MongoDB Examples
- PHP & MongoDB - Connect Database
- PHP & MongoDB - Show Databases
- PHP & MongoDB - Drop Database
- PHP & MongoDB - Create Collection
- PHP & MongoDB - Drop Collection
- PHP & MongoDB - Display Collections
- PHP & MongoDB - Insert Document
- PHP & MongoDB - Select Document
- PHP & MongoDB - Update Document
- PHP & MongoDB - Delete Document
- PHP & MongoDB - Embedded Documents
- PHP & MongoDB - Error Handling
- PHP & MongoDB - Limiting Records
- PHP & MongoDB - Sorting Records
- PHP & MongoDB Useful Resources
- PHP & MongoDB - Quick Guide
- PHP & MongoDB - Useful Resources
- PHP & MongoDB - Discussion
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 MongoDB\Driver\Manager("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 MongoDB\Driver\Command(["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.
Advertisements
To Continue Learning Please Login
Login with Google