MongoDB with PHP James Tang <james@fwso.cn> A quick introduction
What is MongoDB
NoSQL <ul><li>MongoDB is a document-oriented database </li><ul><li>Store data as structured documents </li></ul><li>Docume...
Distributed computing </li></ul></ul>
Why MongoDB
Agility MongoDB simplifies development. Data in MongoDB is stored in JSON-like documents with dynamic schemas( free schema...
Scalability & Performance <ul><li>Build-in support for horizontal availability
Auto-sharding </li><ul><li>Save different parts of data on different machines </li></ul><li>Replication:  backup, failover...
Replica Sets:  automatic failover, no single master </li></ul><li>MapReduce </li></ul>
How MongoDB works
BSON MongoDB use BSON as the storage format.  BSON has following characteristics: <ul><li>Efficiency
Traversability
Performance </li></ul>Reference: http://bsonspec.org/
Data Files <ul><li>BSON objects store in  collections
db_name.collection_name called  namespace
.ns  file used to store metadata for namespace.
Dbname.0, .1, .2... stores the data
The new file double the file size, up to 2GB </li><ul><li>blog.0(16M), blog.1(32M)... </li></ul><li>MongoDB  preallocates ...
Memory Management <ul><li>Memory-mapped storage engine </li><ul><li>Maps all it's data files in memory
The virtual  size of mongod process may very large </li></ul><li>Leave other works to operating system
32bit MongoDB server limit to 2G data per mongod instance </li></ul>
Wire Protocol Clients use wire protocol to access MongoDB server. Wire Protocol is a TCP/IP socket based lightweight proto...
Getting Started
Installation <ul><li>Very easy, just download, and extract
Start server: /path/to/mongodb/bin/mongod </li><ul><li>Default: localhost:27017 </li></ul><li>./mongod -- port  27017 -- m...
Shell Client <ul><li>./mongo [localhost:27017/test]
use blog
db.posts.insert({“title”:”Hello, MongoDB”})
db.posts.find()
db.posts.remove([criteria])
db.posts.drop()
... </li></ul>
PHP driver <ul><li>pecl install mongo
Upcoming SlideShare
Loading in...5
×

Introduction to MongoDB with PHP

12,008

Published on

Quick introduction to MongoDB, a popular document-oriented database, and MongoDB drivers for PHP.

Published in: Technology
0 Comments
17 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
12,008
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
0
Comments
0
Likes
17
Embeds 0
No embeds

No notes for slide
  • /usr/local/mongodb-2.0.1/bin/mongod --port 27017 --dbpath /usr/local/mongodb-2.0.1/data/node1 --replSet cluster/localhost:10001 /usr/local/mongodb-2.0.1/bin/mongod --port 10001 --dbpath /usr/local/mongodb-2.0.1/data/node1 --replSet cluster/localhost:27017 /usr/local/mongodb-2.0.1/bin/mongo localhost:27017/admin
  • Sharding ./mongod --dbpath ~/dbs/config --port 20000 ./mongos --port 30000 --configdb localhost:20000 ./mongod --dbpath ~/dbs/shard1 --port 10001 ./mongod --dbpath ~/dbs/shard2 --port 10002 ./mongo localhost:30000/admin db.runCommand({&amp;quot;addshard&amp;quot; : &amp;quot;localhost:10000&amp;quot;, allowLocal : true}) db.runCommand({&amp;quot;enablesharding&amp;quot; : &amp;quot;blog&amp;quot;}) db.runCommand({&amp;quot;shardcollection&amp;quot; : &amp;quot;blog.posts&amp;quot;, &amp;quot;key&amp;quot; : {&amp;quot;title&amp;quot; : 1}})
  • Introduction to MongoDB with PHP

    1. 1. MongoDB with PHP James Tang <james@fwso.cn> A quick introduction
    2. 2. What is MongoDB
    3. 3. NoSQL <ul><li>MongoDB is a document-oriented database </li><ul><li>Store data as structured documents </li></ul><li>Document-oriented databases are NoSQL </li><ul><li>Apache CouchDB </li></ul><li>Scale horizontally </li><ul><li>Avoid join operations
    4. 4. Distributed computing </li></ul></ul>
    5. 5. Why MongoDB
    6. 6. Agility MongoDB simplifies development. Data in MongoDB is stored in JSON-like documents with dynamic schemas( free schema ), providing flexibility during the development process. Requirements changes so frequently
    7. 7. Scalability & Performance <ul><li>Build-in support for horizontal availability
    8. 8. Auto-sharding </li><ul><li>Save different parts of data on different machines </li></ul><li>Replication: backup, failover, read scaling </li><ul><li>Master-slave replication
    9. 9. Replica Sets: automatic failover, no single master </li></ul><li>MapReduce </li></ul>
    10. 10. How MongoDB works
    11. 11. BSON MongoDB use BSON as the storage format. BSON has following characteristics: <ul><li>Efficiency
    12. 12. Traversability
    13. 13. Performance </li></ul>Reference: http://bsonspec.org/
    14. 14. Data Files <ul><li>BSON objects store in collections
    15. 15. db_name.collection_name called namespace
    16. 16. .ns file used to store metadata for namespace.
    17. 17. Dbname.0, .1, .2... stores the data
    18. 18. The new file double the file size, up to 2GB </li><ul><li>blog.0(16M), blog.1(32M)... </li></ul><li>MongoDB preallocates data files to ensure consistent performance </li></ul>
    19. 19. Memory Management <ul><li>Memory-mapped storage engine </li><ul><li>Maps all it's data files in memory
    20. 20. The virtual size of mongod process may very large </li></ul><li>Leave other works to operating system
    21. 21. 32bit MongoDB server limit to 2G data per mongod instance </li></ul>
    22. 22. Wire Protocol Clients use wire protocol to access MongoDB server. Wire Protocol is a TCP/IP socket based lightweight protocol. Reference: http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol
    23. 23. Getting Started
    24. 24. Installation <ul><li>Very easy, just download, and extract
    25. 25. Start server: /path/to/mongodb/bin/mongod </li><ul><li>Default: localhost:27017 </li></ul><li>./mongod -- port 27017 -- maxConns 50 -- logpath /usr/local/mongodb-2.0.1/var/logs/mongod.log -- pidfilepath /usr/local/mongodb-2.0.1/var/run/mongod.pid -- dbpath /usr/local/mongodb-2.0.1/data/db -- directoryperdb </li></ul>
    26. 26. Shell Client <ul><li>./mongo [localhost:27017/test]
    27. 27. use blog
    28. 28. db.posts.insert({“title”:”Hello, MongoDB”})
    29. 29. db.posts.find()
    30. 30. db.posts.remove([criteria])
    31. 31. db.posts.drop()
    32. 32. ... </li></ul>
    33. 33. PHP driver <ul><li>pecl install mongo
    34. 34. Or dowload and compile
    35. 35. Extenstion address: http://pecl.php.net/package/mongo
    36. 36. Enable at php.ini </li><ul><li>extension=mongo.so </li></ul></ul>
    37. 37. CRUD
    38. 38. Before CRUD <ul><li>Create Database?
    39. 39. Create Collection(table)?
    40. 40. Alter Schema? </li></ul>NO
    41. 41. Before CRUD <ul><li>Connect to Server </li></ul><?php $ mongo = new Mongo( /* ”localhost:27017” */ ); //Instance of `MongoDB`(PHP class here) $ blog = $ mongo -> blog; //Instance of `MongoCollection` $ posts = $ blog -> posts;
    42. 42. Creating
    43. 43. Querying // search for documents where 5 < x < 20 $rangeQuery = array ('views' => array ( '$gt' => 5, '$lt' => 20 )); //Instance of `MongoCursor` class $cursor = $posts -> find($rangeQuery); <ul><li>$collection->find(/*criteria*/, /*fields*/), Query a list </li></ul><ul><li>$collection->findOne(/*criteria*/), Query one </li></ul>// “ _id ” field is always return , except set to false in fields option: //$post->find(array(...), array( '_id'=>false , 'title'));
    44. 44. Querying <ul><li>Query can be complex </li><ul><li>$lt, $lte, $gt, $gte, $not, $in...
    45. 45. $where, array('$where'=>'function(){... return true/false}')
    46. 46. Regular expression, array('title'=>'/^Hello/i')
    47. 47. Limit/Sort with cursor
    48. 48. Aggregation, count/group </li></ul><li>Reference </li><ul><li>http://www.mongodb.org/display/DOCS/Querying
    49. 49. http://www.mongodb.org/display/DOCS/Advanced+Queries </li></ul></ul>
    50. 50. Updating
    51. 51. Updating Using updating modifier: $inc, $set, $push, ...
    52. 52. Why Modifiers <ul><li>Convenient
    53. 53. Atomically – instead of locking and transactions
    54. 54. Reference </li><ul><li>http://www.mongodb.org/display/DOCS/Atomic+Operations
    55. 55. http://www.mongodb.org/display/DOCS/Updating </li></ul></ul>
    56. 56. Upserts Like REPLACE statement in MySQL
    57. 57. Deleting <ul><li>$collection->remove(/*criteria*/)
    58. 58. $collection->drop() </li></ul>
    59. 59. Advanced Features
    60. 60. MapReduce <ul><li>Aggregation parallelized across multiple servers
    61. 61. Count, distinct, group can be done with it
    62. 62. Speed is the cost, not use in “real time” </li></ul>
    63. 63. Replication <ul><li>Master-slave, slave automatically synchronize the data </li><ul><li>./mongod --dbpath ~/dbs/master --port 27017 --master
    64. 64. ./mongod --dbpath ~/dbs/slave --port 10001 --slave --source localhost:27017 </li></ul><li>Replica Sets </li><ul><li>Master-slave, but auto-failover
    65. 65. Not only one master
    66. 66. mongod --port 27017 --dbpath /data/node1 --replSet cluster/localhost:10001
    67. 67. mongod --port 10001 --dbpath /data/node1 --replSet cluster/localhost:27017
    68. 68. db.runCommand({“replSetInitiate”:{“_id”:”cluster”, “members”:[{“_id”:1,”host”:”locahost:27017”},...]}}) </li></ul></ul>
    69. 69. Sharding <ul><li>Store different data in different servers
    70. 70. Sharding key, one key of a collection used to split data
    71. 71. A shard is can be a single server or a replica set </li></ul>
    72. 72. Capped Collection <ul><li>Created in advance and fixed size </li><ul><li>db.createCollection(&quot;my_collection&quot;, {capped: true, size: 100000}); </li></ul><li>Operations like delete, update causes documents to move is disallowed
    73. 73. Old document will be replaced if collection if full
    74. 74. High performance, auto-FIFO, age-out </li><ul><li>Loging, caching, ... </li></ul></ul>
    75. 75. GridFS <ul><li>Store large binary files
    76. 76. Replication, auto-sharding </li><ul><li>Failover, scale-out </li></ul><li>Use mongofiles </li><ul><li>./mongofiles [put|get|list|...] file.png </li></ul><li>PHP: MongoGridFS, MongoGridFSFile, MongoGridFSCursor </li></ul>
    77. 77. The End http://fwso.cn

    ×