• Save
Introduction to MongoDB with PHP
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Introduction to MongoDB with PHP

  • 12,109 views
Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
12,109
On Slideshare
10,909
From Embeds
1,200
Number of Embeds
7

Actions

Shares
Downloads
0
Comments
0
Likes
16

Embeds 1,200

http://fwso.cn 992
http://magento-zend.blogspot.com 125
http://tangobean.com 73
http://cache.baidu.com 7
http://clickjia.lab 1
http://www.schoox.com 1
http://magento-zend.blogspot.co.uk 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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({"addshard" : "localhost:10000", allowLocal : true}) db.runCommand({"enablesharding" : "blog"}) db.runCommand({"shardcollection" : "blog.posts", "key" : {"title" : 1}})

Transcript

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