Download - MongoUK - PHP Development
![Page 2: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/2.jpg)
Server Density Monitoring
Processing Database UI
www.serverdensity.com2/24
![Page 3: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/3.jpg)
13 months ago
Why we moved: http://bit.ly/mysqltomongo3/24
![Page 4: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/4.jpg)
1. Install
pecl install mongo
4/24
![Page 5: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/5.jpg)
2. Connect
$mongo = new Mongo(‘a.example.com,b.example.com’,
array('persist' => ''));
5/24
![Page 6: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/6.jpg)
2. Connect
mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db
6/24
![Page 7: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/7.jpg)
Replica Pairs
$mongo = new Mongo(‘a.example.com,b.example.com’,
array('persist' => ''));
7/24
![Page 8: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/8.jpg)
Replica Pairs = Failover
Master ADC1
16GB RAM
Slave ADC2
16GB RAM
Replica Pair
Master BDC1
16GB RAM
Slave BDC2
16GB RAM
Replica Pair
8/24
![Page 9: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/9.jpg)
Persistent Connections
$mongo = new Mongo(‘a.example.com,b.example.com’,
array('persist' => ''));
9/24
![Page 10: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/10.jpg)
Persistent Connections
$mongo = new Mongo(‘a.example.com,b.example.com’,
array('persist' => 'owl'));
10/24
![Page 11: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/11.jpg)
db.stats()
Documents 937,393,315
Collections 27,566
Indexes 45,277
Stored data 638GB
Inserts 5000-8000/s
As of 17th Jun 2010.11/24
![Page 12: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/12.jpg)
3. Query
$serversCollection = $mongo->selectCollection(‘servers’);
$server = $serversCollection->findOne(array(‘os’ => ‘windows’), array(‘_id’, ‘name’, ‘ip’));
12/24
![Page 13: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/13.jpg)
3. Query
$serversCollection = $mongo->selectCollection(‘servers’);
$server = $serversCollection->findOne(array(‘os’ => ‘windows’), array(‘_id’, ‘name’, ‘ip’));
13/24
![Page 14: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/14.jpg)
3. Query
$serversCollection = $mongo->selectCollection(‘servers’);
$server = $serversCollection->findOne(array(‘os’ => ‘windows’), array(‘_id’, ‘name’, ‘ip’));
14/24
![Page 15: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/15.jpg)
Docs
www.php.net/mongo
15/24
![Page 16: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/16.jpg)
Abstraction/Map layers
http://www.mongodb.org/display/DOCS/PHP+Language+Center
•ActiveMongo •Doctrine•Mango•Vork
16/24
![Page 17: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/17.jpg)
MongoDate
MongoDB Equivalent
new MongoDate() time()
new MongoDate(strtotime(‘now’)) time()
17/24
![Page 18: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/18.jpg)
MongoDate
$from = new MongoDate(strtotime(‘2010-06-18 00:00:00’)); $to = new MongoDate(strtotime(‘2010-06-18 23:59:59’));
$collection->find(array(‘added’ => array('$gt' => $from, '$lte' => $to)));
18/24
![Page 19: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/19.jpg)
MongoId
$doc = $collection->findOne(array(‘_id’ => ‘4b74ae0d064b35442948da4c’));
19/24
![Page 20: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/20.jpg)
MongoId
$id = new MongoId(‘4b74ae0d064b35442948da4c’);
$doc = $collection->findOne(array(‘_id’ => $id));
20/24
![Page 21: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/21.jpg)
MongoId & remove()
Slow(er):$collection->remove(array(‘key1’ => 5, ‘key2’ => 6));
Fast(er):$cursor = $collection->find(array(‘key1’ => 5, ‘key2’ => 6));
$while ($cursor->hasNext()) { $item = $cursor->getNext();$key = new MongoId($cursor->key());$collection->remove(array('_id' => $key));
}21/24
![Page 22: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/22.jpg)
Safe insert()
$collection->insert($doc, array(‘safe’));
22/24
![Page 23: MongoUK - PHP Development](https://reader036.vdocuments.us/reader036/viewer/2022081400/555a699ad8b42ae7218b4d44/html5/thumbnails/23.jpg)
Docs
www.php.net/mongo
23/24