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. My Code
My code for Cakephp
http://github.com/ichikaway
6. TOPIC
1. Who am I
2. MongoDB
3. PHP + MongoDB
4. CakePHP + MongoDB
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. 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 ]
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
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. MongoDB Fit
• Need Performance
– Read/Write is fast
• Scale out
– Master/Slave
– Replica set(Automatic Failover)
– Sharding
27. SQL to Mongo Mapping Chart
http://www.mongodb.org/display/DOCS/SQL+to+Mongo+
Mapping+Chart
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. Cool Stuffs of MongoDB
Geospatial Index
Capped Collection
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. MongoDB Geospatial Index
Insert data
db.geotest.save({ loc:[ 35, 139 ] })
Latitude Longitude
create Index
db.geotest.ensureIndex( {loc:”2d”} )
32. MongoDB Geospatial Index
Find the point
db.geotest.find({ loc:[35, 140] });
$near
db.geotest.find({ loc:{ $near : [30, 130] } })
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});
42. TOPIC
1. Who am I
2. MongoDB
3. PHP + MongoDB
4. CakePHP + MongoDB
43. MongoDB Datasource
Developed since Jan, 2010
PHP5+
Pecl Mongo
CakePHP1.2+
– Recommend Cake1.3
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. Cake Datasource
The link between models and the
source(DB)
Transparency
– Use Mongo from Model methods
– connect to MongoDB and use
collection automatically
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. 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
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}
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. Summary
MongoDB
- Good case
- Query
- Geospatial Index
- Capped Collection
PHP + MongoDB
CakePHP + MongoDB