-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-test.php
33 lines (27 loc) · 894 Bytes
/
mongo-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// execute query
// retrieve all documents
$cursor = $collection->find();
// iterate through the result set
// print each document
echo $cursor->count() . ' document(s) found. <br/>' . "\n";
foreach ($cursor as $obj) {
echo 'Name: ' . $obj['name'] . '<br/>' . "\n";
echo 'Quantity: ' . $obj['quantity'] . '<br/>' . "\n";
echo 'Price: ' . $obj['price'] . '<br/>' . "\n";
echo '<br/>' . "\n";
}
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}