Easy Tutorial
❮ Mongodb Limit Skip Mongodb Indexing Limitations ❯

MongoDB PHP

To use MongoDB in PHP, you must use the MongoDB PHP driver.

For installation on various platforms and driver package downloads, please refer to: PHP Installation of MongoDB Extension Driver.

If you are using PHP 7, please see: PHP7 MongoDB Installation and Usage.

Ensuring Connection and Selecting a Database

To ensure a correct connection, you need to specify the database name. If the database does not exist in MongoDB, MongoDB will automatically create it.

Here is a code snippet:

<?php
$m = new MongoClient(); // Connects to the default host and port: mongodb://localhost:27017
$db = $m->test; // Gets the database named "test"
?>

Creating a Collection

Here is a code snippet to create a collection:

<?php
$m = new MongoClient(); // Connect
$db = $m->test; // Gets the database named "test"
$collection = $db->createCollection("tutorialpro");
echo "Collection created successfully";
?>

Executing the above program will output:

Collection created successfully

Inserting Documents

Use the insert() method to insert documents in MongoDB.

Here is a code snippet to insert a document:

<?php
$m = new MongoClient();    // Connect to MongoDB
$db = $m->test;            // Select a database
$collection = $db->tutorialpro; // Select a collection
$document = array( 
    "title" => "MongoDB", 
    "description" => "database", 
    "likes" => 100,
    "url" => "http://www.tutorialpro.org/mongodb/",
    "by" => "tutorialpro.org"
);
$collection->insert($document);
echo "Data inserted successfully";
?>

Executing the above program will output:

Data inserted successfully

You can then view the data in the mongo client using the command db.tutorialpro.find().pretty();.


Finding Documents

Use the find() method to read documents in the collection.

Here is a code snippet to read documents:

<?php
$m = new MongoClient();    // Connect to MongoDB
$db = $m->test;            // Select a database
$collection = $db->tutorialpro; // Select a collection

$cursor = $collection->find();
// Iterate and display document titles
foreach ($cursor as $document) {
    echo $document["title"] . "\n";
}
?>

Executing the above program will output:

MongoDB

Updating Documents

Use the update() method to update documents.

This example will update the title to 'MongoDB Tutorial'. Here is the code snippet:

<?php
$m = new MongoClient();    // Connect to MongoDB
$db = $m->test;            // Select a database
$collection = $db->tutorialpro; // Select a collection
// Update document
$collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB Tutorial")));
// Display updated documents
$cursor = $collection->find();
// Loop and display document titles
foreach ($cursor as $document) {
    echo $document["title"] . "\n";
}
?>

Executing the above program will output:

MongoDB Tutorial

You can then view the data in the mongo client using the command db.tutorialpro.find().pretty();.


Deleting Documents

Use the remove() method to delete documents.

This example will remove the record with the title 'MongoDB Tutorial'. Here is the code snippet:

<?php
$m = new MongoClient();    // Connect to MongoDB
$db = $m->test;            // Select a database
$collection = $db->tutorialpro; // Select a collection
// Remove document
$collection->remove(array("title"=>"MongoDB Tutorial"), array("justOne" => true));
echo "Document deleted successfully";
?>

Executing the above program will output:

Document deleted successfully
$m = new MongoClient();    // Connect to MongoDB
$db = $m->test;            // Select a database
$collection = $db->tutorialpro; // Select a collection

// Remove document
$collection->remove(array("title"=>"MongoDB Tutorial"), array("justOne" => true));

// Display available document data
$cursor = $collection->find();
foreach ($cursor as $document) {
    echo $document["title"] . "\n";
}
?>

In addition to the above examples, in PHP you can also use methods like findOne(), save(), limit(), skip(), sort(), etc., to manipulate the MongoDB database.

For more operations, refer to the MongoDB core classes: http://php.net/manual/en/mongo.core.php.

❮ Mongodb Limit Skip Mongodb Indexing Limitations ❯