PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Tutorial

Table of Contents

This is the official MongoDB driver for PHP.

Here's a quick code sample that connects, inserts documents, queries for documents, iterates through query results, and disconnects from MongoDB. There are more details on each step in the tutorial below.

<?php

// connect
$m = new MongoClient();

// select a database
$db $m->comedy;

// select a collection (analogous to a relational database's table)
$collection $db->cartoons;

// add a record
$document = array( "title" => "Calvin and Hobbes""author" => "Bill Watterson" );
$collection->insert($document);

// add another record, with a different "shape"
$document = array( "title" => "XKCD""online" => true );
$collection->insert($document);

// find everything in the collection
$cursor $collection->find();

// iterate through the results
foreach ($cursor as $document) {
    echo 
$document["title"] . "\n";
}

?>

The above example will output:

Calvin and Hobbes
XKCD

PHP Manual