24
PHP Development David Mytton [email protected] / @davidmytton 1/24

MongoUK - PHP Development

Embed Size (px)

Citation preview

Page 1: MongoUK - PHP Development

PHP Development

David [email protected] / @davidmytton1/24

Page 2: MongoUK - PHP Development

Server Density Monitoring

Processing Database UI

www.serverdensity.com2/24

Page 3: MongoUK - PHP Development

13 months ago

Why we moved: http://bit.ly/mysqltomongo3/24

Page 4: MongoUK - PHP Development

1. Install

pecl install mongo

4/24

Page 5: MongoUK - PHP Development

2. Connect

$mongo = new Mongo(‘a.example.com,b.example.com’,

array('persist' => ''));

5/24

Page 6: MongoUK - PHP Development

2. Connect

mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db

6/24

Page 7: MongoUK - PHP Development

Replica Pairs

$mongo = new Mongo(‘a.example.com,b.example.com’,

array('persist' => ''));

7/24

Page 8: MongoUK - PHP Development

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

Persistent Connections

$mongo = new Mongo(‘a.example.com,b.example.com’,

array('persist' => ''));

9/24

Page 10: MongoUK - PHP Development

Persistent Connections

$mongo = new Mongo(‘a.example.com,b.example.com’,

array('persist' => 'owl'));

10/24

Page 11: MongoUK - PHP Development

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

3. Query

$serversCollection = $mongo->selectCollection(‘servers’);

$server = $serversCollection->findOne(array(‘os’ => ‘windows’), array(‘_id’, ‘name’, ‘ip’));

12/24

Page 13: MongoUK - PHP Development

3. Query

$serversCollection = $mongo->selectCollection(‘servers’);

$server = $serversCollection->findOne(array(‘os’ => ‘windows’), array(‘_id’, ‘name’, ‘ip’));

13/24

Page 14: MongoUK - PHP Development

3. Query

$serversCollection = $mongo->selectCollection(‘servers’);

$server = $serversCollection->findOne(array(‘os’ => ‘windows’), array(‘_id’, ‘name’, ‘ip’));

14/24

Page 15: MongoUK - PHP Development

Docs

www.php.net/mongo

15/24

Page 16: MongoUK - PHP Development

Abstraction/Map layers

http://www.mongodb.org/display/DOCS/PHP+Language+Center

•ActiveMongo •Doctrine•Mango•Vork

16/24

Page 17: MongoUK - PHP Development

MongoDate

MongoDB Equivalent

new MongoDate() time()

new MongoDate(strtotime(‘now’)) time()

17/24

Page 18: MongoUK - PHP Development

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

MongoId

$doc = $collection->findOne(array(‘_id’ => ‘4b74ae0d064b35442948da4c’));

19/24

Page 20: MongoUK - PHP Development

MongoId

$id = new MongoId(‘4b74ae0d064b35442948da4c’);

$doc = $collection->findOne(array(‘_id’ => $id));

20/24

Page 21: MongoUK - PHP Development

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

Safe insert()

$collection->insert($doc, array(‘safe’));

22/24

Page 23: MongoUK - PHP Development

Docs

www.php.net/mongo

23/24

Page 24: MongoUK - PHP Development

David [email protected] / @davidmytton

Slides

blog.boxedice.com/mongodb

24/24