PHP Driver Examples

The PHP driver is an official driver maintained by MongoDB. It is written in C, and is currently compatible with the following versions of MongoDB:

PHP Driver MongoDB 2.4 MongoDB 2.6 MongoDB 3.0
1.6
1.5  

The current version of the PHP driver supports the following versions of PHP:

PHP Driver PHP 5.3 PHP 5.4 PHP 5.5 PHP 5.6
1.6
1.5

Installation

To use the PHP driver you’ll need to first install it via pecl:

$ sudo pecl install mongo

Then ensure it’s enabled in your php.ini with:

extension=mongo.so

Note that if you’ve installed the driver via a package manager like yum the configuration may be in a standalone file in your php.d directory or similar.

Example document

Here’s the example document we’ll be using:

{
    "start": "2015-09-02T22:46:30.782Z",
    "end": "2016-09-02T22:46:30.782Z",
    "location": "Texas",
    "official_game": false,
    "winner": "Javi",
    "players": [
        {
            "name": "Javi",
            "decks": [
                "Dinosaurs",
                "Plants"
            ],
            "points": 24,
            "place": 1
        },
        {
            "name": "Seth",
            "decks": [
                "Spies",
                "Zombies"
            ],
            "points": 20,
            "place": 2
        },
        {
            "name": "Dave",
            "decks": [
                "Steampunk",
                "Wizard"
            ],
            "points": 20,
            "place": 2
        },
        {
            "name": "Castro",
            "decks": [
                "Shapeshifters",
                "Ninjas"
            ],
            "points": 18,
            "place": 4
        }
    ]
}

Connecting

Warning

When connecting using the MongoDB URI, we highly recommend avoiding usernames or passwords with an @ symbol inside. This can break the URI parsing and cause failures when trying to connect.

Connecting to a replica set:

<?php
$connection = new MongoClient("mongodb://myUsername:myPassword@sjc-c9-1.objectrocket.com:12345,sjc-c9-0.objectrocket.com:54074/databaseName?replicaSet=e0a8d0f797be1b9c4ec7052a7b7484a7");
?>

Connecting to a sharded instance:

<?php
$connection = new MongoClient("mongodb://myUsername:myPassword@iad-mongos0.objectrocket.com:12345/databaseName");
?>

Connecting to a sharded instance with SSL:

<?php
$connection = new MongoClient("mongodb://myUsername:myPassword@iad-mongos0.objectrocket.com:12345/databaseName", array("ssl" => true));
?>

Creating a Document

Creating and inserting the document:

<?php

$doc = array(
   "date" => new MongoDate(strtotime("2014-05-26 02:00:22")),
     "winner" => "Javi",
     "logged" => TRUE,
     "decks" => array( "first" => array("Dinosaurs","Plants"), "second" => array("Spies","Zombies"), "third" => array("Steampunk","Wizards"), "fourth" => array("Shapeshifters", "Ninjas")),
     "prior_winner" => "Castro",
     "points" => array( 24, 20, 20, 18),
     "players" => array( "first" => "Javi", "second" => "Seth", "third" => "Dave", "fourth" => "Castro")
     );

$connection = new MongoClient("mongodb://myUsername:myPassword@hkg-mongos0.objectrocket.com:12345/myDatabaseName");

$database = $connection->myDatabaseName;

$collection = $database->myCollectionName;

$collection->insert( $doc );

?>

The resulting document seen through the MongoDB shell:

> db.myCollectionName.find().pretty()
{
       "_id" : ObjectId("55b29160d5d145e1438b4567"),
       "date" : ISODate("2014-05-26T02:00:22Z"),
       "winner" : "Javi",
       "logged" : true,
       "decks" : {
               "first" : [
                       "Dinosaurs",
                       "Plants"
               ],
               "second" : [
                       "Spies",
                       "Zombies"
               ],
               "third" : [
                       "Steampunk",
                       "Wizards"
               ],
               "fourth" : [
                       "Shapeshifters",
                       "Ninjas"
               ]
       },
       "prior_winner" : "Castro",
       "points" : [
               NumberLong(24),
               NumberLong(20),
               NumberLong(20),
               NumberLong(18)
       ],
       "players" : {
               "first" : "Javi",
               "second" : "Seth",
               "third" : "Dave",
               "fourth" : "Castro"
       }
}

Reading documents

Finding all documents with a specific field:

<?php

$connection = new MongoClient("mongodb://myUsername:myPassword@hkg-mongos0.objectrocket.com:12345/myDatabaseName");

$database = $connection->myDatabaseName;

$collection = $database->myCollectionName;


$query = array("winner" => "Javi");

$cursor = $collection->find($query);
foreach ($cursor as $doc) {
   var_dump($doc);
}

?>

Output from above:

array(8) {
 ["_id"]=>
 object(MongoId)#7 (1) {
   ["$id"]=>
   string(24) "55b29160d5d145e1438b4567"
 }
 ["date"]=>
 object(MongoDate)#8 (2) {
   ["sec"]=>
   int(1401069622)
   ["usec"]=>
   int(0)
 }
 ["winner"]=>
 string(4) "Javi"
 ["logged"]=>
 bool(true)
 ["decks"]=>
 array(4) {
   ["first"]=>
   array(2) {
     [0]=>
     string(9) "Dinosaurs"
     [1]=>
     string(6) "Plants"
   }
   ["second"]=>
   array(2) {
     [0]=>
     string(5) "Spies"
     [1]=>
     string(7) "Zombies"
   }
   ["third"]=>
   array(2) {
     [0]=>
     string(9) "Steampunk"
     [1]=>
     string(7) "Wizards"
   }
   ["fourth"]=>
   array(2) {
     [0]=>
     string(13) "Shapeshifters"
     [1]=>
     string(6) "Ninjas"
   }
 }
 ["prior_winner"]=>
 string(6) "Castro"
 ["points"]=>
 array(4) {
   [0]=>
   int(24)
   [1]=>
   int(20)
   [2]=>
   int(20)
   [3]=>
   int(18)
 }
 ["players"]=>
 array(4) {
   ["first"]=>
   string(4) "Javi"
   ["second"]=>
   string(4) "Seth"
   ["third"]=>
   string(4) "Dave"
   ["fourth"]=>
   string(6) "Castro"
 }
}

Updating a document

Updating a document:

<?php

$connection = new MongoClient("mongodb://myUsername:myPassword@hkg-mongos0.objectrocket.com:12345/myDatabaseName");

$database = $connection->myDatabaseName;

$collection = $database->myCollectionName;


$retval = $collection->findAndModify(
   array("winner" => "Javi", "logged" => TRUE),
   array('$set' => array("winner" => "Castro", "logged" => FALSE, "players.first" => "Castro", "players.fourth" => "Javi")),
   null,
   array("new" => TRUE)
);

?>

The resulting document as seen from the MongoDB shell:

> db.myCollectionName.find().pretty()
{
       "_id" : ObjectId("55b29b5ed5d145014f8b4567"),
       "date" : ISODate("2014-05-26T02:00:22Z"),
       "decks" : {
               "first" : [
                       "Dinosaurs",
                       "Plants"
               ],
               "second" : [
                       "Spies",
                       "Zombies"
               ],
               "third" : [
                       "Steampunk",
                       "Wizards"
               ],
               "fourth" : [
                       "Shapeshifters",
                       "Ninjas"
               ]
       },
       "logged" : false,
       "players" : {
               "first" : "Castro",
               "fourth" : "Javi",
               "second" : "Seth",
               "third" : "Dave"
       },
       "points" : [
               NumberLong(24),
               NumberLong(20),
               NumberLong(20),
               NumberLong(18)
       ],
       "prior_winner" : "Castro",
       "winner" : "Castro"
}

Deleting a document

Deleting a document:

<?php

$connection = new MongoClient("mongodb://myUsername:myPassword@hkg-mongos0.objectrocket.com:12345/myDatabaseName");

$database = $connection->myDatabaseName;

$collection = $database->myCollectionName;


$query = array("winner" => "Castro");

$retval = $collection->remove($query);

var_dump($retval);

?>

Output from above:

array(6) {
 ["singleShard"]=>
 string(161) "0c86375ef57646f094a0a27164679c33/hkgclus1br0vz17.hkg.objectrocket.com:12345,hkgclus1br1vz17.hkg.objectrocket.com:32728,hkgclus1br2vz17.hkg.objectrocket.com:32728"
 ["n"]=>
 int(1)
 ["lastOp"]=>
 object(MongoTimestamp)#6 (2) {
   ["sec"]=>
   int(1437769866)
   ["inc"]=>
   int(1)
 }
 ["connectionId"]=>
 int(64925)
 ["err"]=>
 NULL
 ["ok"]=>
 float(1)
}

Additional reading

If you need more help with the PHP driver, links to official documentation are below: