PHP Portal » PHP Handbuch » Beispiele

Werbung

Beispiele


This example shows how to connect, insert objects, query for objects, iterate through query results, and disconnect from a Mongo database.

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// connect $m = new Mongo(); // select a database $db = $m->comedy; $collection = $db->cartoons; // add an element $obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" ); $collection->insert($obj); // add another element, with a different "shape" $obj = array( "title" => "XKCD", "online" => true ); $collection->insert($obj); // find everything in the collection $cursor = $collection->find(); // iterate through the results foreach ($cursor as $obj) { echo $obj["title"] . "\n"; } // disconnect $m->close();

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Calvin and Hobbes
XKCD