MongoDB PHP Integration
MongoDB provides a PHP driver that allows us to interact with MongoDB in PHP applications. Through the PHP driver, we can perform various operations such as inserting, querying, updating, and deleting data.
Basic Concepts
Types of PHP Drivers
- MongoDB PHP Driver: Official PHP driver provided by MongoDB.
- MongoDB ODM: Object Document Mapper based on MongoDB PHP Driver, providing a more advanced API.
In this chapter, we will use the official PHP driver.
Connecting to MongoDB
To connect to MongoDB, we need to use the MongoDB\Client class.
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
// Connect to MongoDB server
$client = new Client('mongodb://localhost:27017');
// Get database
$database = $client->mydatabase;
echo "Connected to database successfully";
} catch (Exception $e) {
echo $e->getMessage();
}
?>Inserting Data
Inserting a Single Document
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Create document
$document = [
'name' => 'John',
'age' => 30,
'email' => 'john@example.com',
'status' => 'active'
];
// Insert document
$collection->insertOne($document);
echo "Document inserted successfully";
} catch (Exception $e) {
echo $e->getMessage();
}
?>Inserting Multiple Documents
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Create document list
$documents = [
[
'name' => 'Jane',
'age' => 25,
'email' => 'jane@example.com',
'status' => 'pending'
],
[
'name' => 'Bob',
'age' => 35,
'email' => 'bob@example.com',
'status' => 'inactive'
]
];
// Insert document list
$collection->insertMany($documents);
echo "Documents inserted successfully";
} catch (Exception $e) {
echo $e->getMessage();
}
?>Querying Data
Querying All Documents
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Query all documents
$documents = $collection->find();
// Iterate through results
foreach ($documents as $document) {
var_dump($document);
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>Querying Specific Documents
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Query specific document
$filter = ['name' => 'John'];
$documents = $collection->find($filter);
// Iterate through results
foreach ($documents as $document) {
var_dump($document);
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>Updating Data
Updating a Single Document
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Query;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Update single document
$filter = ['name' => 'John'];
$update = ['$set' => ['age' => 31]];
$collection->updateOne($filter, $update);
echo "Document updated successfully";
} catch (Exception $e) {
echo $e->getMessage();
}
?>Updating Multiple Documents
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Update multiple documents
$filter = ['status' => 'active'];
$update = ['$set' => ['status' => 'pending']];
$collection->updateMany($filter, $update);
echo "Documents updated successfully";
} catch (Exception $e) {
echo $e->getMessage();
}
?>Deleting Data
Deleting a Single Document
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Delete single document
$filter = ['name' => 'Bob'];
$collection->deleteOne($filter);
echo "Document deleted successfully";
} catch (Exception $e) {
echo $e->getMessage();
}
?>Deleting Multiple Documents
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Delete multiple documents
$filter = ['status' => 'pending'];
$collection->deleteMany($filter);
echo "Documents deleted successfully";
} catch (Exception $e) {
echo $e->getMessage();
}
?>Advanced Querying
Using Query Operators
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Use query operators
$filter = [
'$and' => [
['age' => ['$gt' => 25]],
['status' => 'active']
]
];
$documents = $collection->find($filter);
// Iterate through results
foreach ($documents as $document) {
var_dump($document);
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>Sorting and Pagination
php
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
try {
$client = new Client('mongodb://localhost:27017');
$database = $client->mydatabase;
$collection = $database->mycollection;
// Sorting and pagination
$documents = $collection->find(
[],
[
'sort' => ['age' => -1],
'skip' => 1,
'limit' => 2
]
);
// Iterate through results
foreach ($documents as $document) {
var_dump($document);
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>Summary
The MongoDB PHP driver allows us to interact with MongoDB in PHP applications. Through the PHP driver, we can perform various operations such as inserting, querying, updating, and deleting data. When using the PHP driver, we should pay attention to connection management, data processing, and error handling to ensure the efficient operation of the application.