How to use MongoDB with CakePHP

26,058 views

Published on

Cakefest2010 ichikaway, How to use MongoDB with CakePHP

Published in: Technology
1 Comment
40 Likes
Statistics
Notes
  • how to use distinct and group by in Cakephp with MOngoDB
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
26,058
On SlideShare
0
From Embeds
0
Number of Embeds
497
Actions
Shares
0
Downloads
0
Comments
1
Likes
40
Embeds 0
No embeds

No notes for slide

How to use MongoDB with CakePHP

  1. 1. How to use MongoDB with CakePHP 2010/9/4 Cakefest2010 Yasushi Ichikawa
  2. 2. TOPIC 1. Who am I 2. MongoDB 3. PHP + MongoDB 4. CakePHP + MongoDB
  3. 3. Yasushi Ichikawa Twitter: @ichikaway Call me ichi
  4. 4. My Code • I've used Cakephp since Aug 2008. • Author of the SQL Explain Component → One of Debug_kit Contributors • Author of the Cakephp MongoDB-Datasource
  5. 5. My Code  My code for Cakephp  http://github.com/ichikaway
  6. 6. TOPIC 1. Who am I 2. MongoDB 3. PHP + MongoDB 4. CakePHP + MongoDB
  7. 7. MongoDB is One of NoSQL
  8. 8. MongoDB • Open-source non-relational DB • C++ • GNU AGPL v3.0 • Many Drivers(PHP, Perl, Ruby, etc) – Apache License • Company: 10gen • Download (version 1.6.2) – http://www.mongodb.org/display/DOCS/Downloads
  9. 9. MongoDB TERMs RDB MongoDB Table Collection Row Document Column Field
  10. 10. MongoDB Position • Performance – Key/Value > mongoDB >>> RDBMS • Functionality – RDBMS > mongoDB >>> Key/Value
  11. 11. MongoDB Provides • Scalability • Queryable • Read/Write FAST • Schema Free
  12. 12. MongoDB Schema • Document: bson (similar json) – Binary data • Schema Free – different schema in same collection Id, title, body Id, name, tel, fax Id, name, nickname, email Posts collection
  13. 13. MongoDB Schema • Embedded Object Posts collection id: 1, name: ichikaway, tel: 090-1111-2222. fax: Address skill Country: Japan, Java: 2years, Zip: 222-3333 PHP: 3years id: 2, name: Yasushi, tel: 090-3333-4444. fax: id: 3, name: Ninjaaa, tel: 090-5555-6666. fax:
  14. 14. RDB Schema • RDB schema • Natural data structure?? Screen Blog Blog table Tags table Title Title Tag1 Text Text Tag2 Tag3 tag1,tag2,tag3 RDB Comment Table Comment1 Comment2 Comment1 Comment3 Comment2 Comment3
  15. 15. MongoDB schema • Schema Free – Natural data structure Screen Blog Blog collection Title Title : xxxx Text Text : yyyy tag1,tag2,tag3 Mongo Tag: [tag1,tag2,tag3] Comment: [comment1, Comment1 comment2, Comment2 comment3 Comment3 ]
  16. 16. MongoDB In Production github, heroku(MongoHQ), etc
  17. 17. MongoDB Production Case • Business insider • http://www.businessinsider.com/how-we-use-mongodb-2009-11 • Over 600,000 PV / business day • 3 apache + 1 mongoDB • MongoDB uses under 5% cpu time
  18. 18. MongoDB Fit or Not Not replace all RDBMS
  19. 19. MongoDB Not FIT • Need Join • Need Transaction • Small disk space – each record has field label data Posts collection id: 1, name: ichikaway, tel: 090-1111-2222. fax: 6bytes id: 2, name: Yasushi, Age: 20 . Tel: 111-2222 id: 3, name: Ninjaaa, Language: Japanese
  20. 20. MongoDB Fit • Need Performance – Read/Write is fast • Scale out – Master/Slave – Replica set(Automatic Failover) – Sharding
  21. 21. How to use
  22. 22. MongoDB Retrieving Data SELECT field1, field2 From blog WHERE field1 = 'aaa'; db.blog.find( { field1 : ”aaa” }, { field1 : 1, field2 : 1 } ) Collection name
  23. 23. MongoDB Retrieving Data SELECT * WHERE field1 > 3 ORDER BY field2 DESC LIMIT 10, 20; db.collection.find( { field1 : { $gt:3 } } ) .sort(field2: -1) .skip(10) .limit(20);
  24. 24. MongoDB find operators • $gt, $gte • $lt, $lte • $ne • $in • $nin • $or http://www.mongodb.org/display/DOCS/Advanced+Queries
  25. 25. MongoDB find tips • Regular Expressions – { name : /ichikawa.*sushi/i } • Embedded Object – { "author.name" : "ichikawa" } Blog collection Title : xxxx Text : yyyy author: { name : 'ichikawa', age : 15 }
  26. 26. MongoDB DEMO
  27. 27. SQL to Mongo Mapping Chart http://www.mongodb.org/display/DOCS/SQL+to+Mongo+ Mapping+Chart
  28. 28. MongoDB Indexing Create Index db.collection.ensureIndex({“title” : 1}) Delete Index db.collection.dropIndex({“title” : 1}) Create Embedded Doc Index db.collection.ensureIndex ({“Autor.name” : 1}) Create Index(Background) db.collection.ensureIndex ({“title” : 1}, {background:true} )
  29. 29. Cool Stuffs of MongoDB Geospatial Index Capped Collection
  30. 30. MongoDB Geospatial Index  Version 1.3.3+ , recommend 1.6.2+ (Bug fix)  Calculate location data – Latitude(緯度), Longitude(経度)  find the closest location with Operators – $near: sort from the closest point – $box: find points within the rectangle – $center: find points within the circle
  31. 31. MongoDB Geospatial Index  Insert data db.geotest.save({ loc:[ 35, 139 ] }) Latitude Longitude  create Index db.geotest.ensureIndex( {loc:”2d”} )
  32. 32. MongoDB Geospatial Index  Find the point db.geotest.find({ loc:[35, 140] });  $near db.geotest.find({ loc:{ $near : [30, 130] } })
  33. 33. MongoDB Geospatial Index  $box – lower left, upper right db.geotest.find({ loc:{ $within:{ $box: [[1, 1], [10,10]] }}});  $center – set radius(degree) db.geotest.find({ loc:{ $within:{ $center: [[10, 10], 0.1 ] } radius(degree) }})
  34. 34. MongoDB Geospatial Index DEMO
  35. 35. MongoDB Capped Collection  Creating a Fixed Size (capped) Collection  Not think about data space / overflow • Age out data is deleted when Collection is full • Good for – Logging – Caching db.createCollection("testcap1", {capped: true, size:5000});
  36. 36. MongoDB Attension  32bit version limitation –can't save data over 2Gbytes
  37. 37. MongoDB Books http://www.mongodb.org/display/DOCS/Books
  38. 38. TOPIC 1. Who am I 2. MongoDB 3. PHP + MongoDB 4. CakePHP + MongoDB
  39. 39. Pecl Mongo Driver • Apache License • Setup pecl install mongo extension=mongo.so (php.ini) • Manual –http://us2.php.net/mongo
  40. 40. Pecl Mongo • Connect $mongo = new Mongo(localhost:27017); $db = $mongo->selectDB('blog'); • select collection $collection = $db->selectCollection('posts'); • Find $collection->find($cond, $filed) ->sort()->limit(5)->skip();
  41. 41. Pecl Mongo $data = array('field1' => 'val1', 'field2' => 'val2'); • Insert $collection->insert($data); • Update $new = array('$set' => array('field2' => 'aa')); $cond = array('field1' => 'val1'); $collection->update($cond, $new);
  42. 42. TOPIC 1. Who am I 2. MongoDB 3. PHP + MongoDB 4. CakePHP + MongoDB
  43. 43. MongoDB Datasource  Developed since Jan, 2010  PHP5+  Pecl Mongo  CakePHP1.2+ – Recommend Cake1.3
  44. 44. MongoDB Datasource  Version 0.3 – set schema info to Model::schema – test cases  Version 0.4 – create schema info from Post data – show command log Huge Thanks AD7six
  45. 45. Cake Datasource  The link between models and the source(DB)  Transparency – Use Mongo from Model methods – connect to MongoDB and use collection automatically
  46. 46. Cake calls Datasource methods Model Method Datasource find() read() save() create() save() update() delete() delete()
  47. 47. Let's use
  48. 48. Setup  cd app/plugins  git clone http://github.com/ichikaway/mongoDB- Datasource.git mongodb  cd app/config  vi database.php
  49. 49. database.php <?php class DATABASE_CONFIG { public $default = array( 'driver' => 'mongodb.mongodbSource', 'database' => 'cakemongo1', 'host' => 'localhost', 'port' => 27017, );
  50. 50. Sample Cake Mongo DEMO Sample1
  51. 51. Sample2
  52. 52. Sample Cake Mongo part 2  Delete History  Main data is stored in MySQL  save deleted records in history table  search history records
  53. 53. Sample Cake Mongo part 2 MySQL App Default Schema CakePHP Post table User table
  54. 54. Sample Cake Mongo part 2  RDB way  copy schema(all table)
  55. 55. Sample Cake Mongo part 2 MySQL App Default Schema CakePHP Post table User table 1. Delete Copy Schema Post table User table 2. save
  56. 56. Sample Cake Mongo part 2  RDB way  copy schema(all table)  Hard to maintain both schema  serialize deleted record, save in history table  Hard to search in history table
  57. 57. Sample Cake Mongo 2 Mongo Way
  58. 58. Sample Cake Mongo part 2 MySQL App Default Schema CakePHP Post table User table 1. Delete MongoDB History Collection {'post' : Post record} 2. save {'user' : User record}
  59. 59. Sample Cake Mongo 2 DEMO
  60. 60. Sample Cake Mongo 2  Good Fit Document Database – save different table records in one collection  Don't need prepare schema before Save  Search easily in History Collection
  61. 61. Summary  MongoDB - Good case - Query - Geospatial Index - Capped Collection  PHP + MongoDB  CakePHP + MongoDB
  62. 62. Thank you! Questions? Please, speak slowly :)

×