-
Notifications
You must be signed in to change notification settings - Fork 0
/
step03.php
35 lines (31 loc) · 873 Bytes
/
step03.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
34
35
<?php
try {
$mongo = new Mongo(); // default host:port
$db = $mongo->example;
$collection = $db->test;
$collection->drop();
$document = array( '_id' => 'gates',
'name' => 'Gaëtan Voyer-Perrault',
'friends' => array('bernie', 'alvin'),
'followers' => 18,
'contact' => array( 'twitter' => '@gatesvp',
'email' => '[email protected]')
);
$collection->insert($document);
// Basic query
$query = array( '_id' => 'gates');
$result = $collection->findOne($query);
print($result['_id']."\n");
// Query on array
$query = array( 'friends' => 'alvin');
$result = $collection->findOne($query);
print($result['_id']."\n");
// Query on sub-document
$query = array( 'contact.twitter' => '@gatesvp');
$result = $collection->findOne($query);
print($result['_id']."\n");
}
catch(Exception $e) {
print($e->getMessage());
}
?>