Her er et enkelt php-skript som kobler seg til en Mongo-database og setter inn et par dokumenter i en samling og lister dem opp etterpå:
<?php
error_reporting(E_ALL);
ini_set(‘display_errors’, ‘1’);
header(“Content-type: text/plain”);
$connection = new Mongo();
$db = $connection->mydb;
$collection = $db->mycollection;
$myobj = array(“first_name” => “John”, “last_name” => “Doe”, “age” => 30);
$myobj2 = array(“first_name” => “Jane”, “last_name” => “Doe”, “age” => 27);
$collection->insert($myobj);
$collection->insert($myobj2);
$cursor = $collection->find();
foreach($cursor as $result)
{
echo “Name: ” . $result[“last_name”] . “, ” . $result[“first_name”] . “\n”;
echo “Age: ” . $result[“age”] . “\n\n”;
}
//$db->drop();
$connection->close();
?>