• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Introduction to MongoDB with PHP
 

Introduction to MongoDB with PHP

on

  • 10,377 views

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.

Statistics

Views

Total Views
10,377
Views on SlideShare
9,227
Embed Views
1,150

Actions

Likes
16
Downloads
0
Comments
0

7 Embeds 1,150

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

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • /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}})

Introduction to MongoDB with PHP Introduction to MongoDB with PHP Presentation Transcript

  • MongoDB with PHP James Tang <james@fwso.cn> A quick introduction
  • What is MongoDB
  • NoSQL
    • MongoDB is a document-oriented database
      • Store data as structured documents
    • Document-oriented databases are NoSQL
      • Apache CouchDB
    • Scale horizontally
      • Avoid join operations
      • Distributed computing
  • Why MongoDB
  • 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
  • Scalability & Performance
    • Build-in support for horizontal availability
    • Auto-sharding
      • Save different parts of data on different machines
    • Replication: backup, failover, read scaling
      • Master-slave replication
      • Replica Sets: automatic failover, no single master
    • MapReduce
  • How MongoDB works
  • BSON MongoDB use BSON as the storage format. BSON has following characteristics:
    • Efficiency
    • Traversability
    • Performance
    Reference: http://bsonspec.org/
  • Data Files
    • 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
      • blog.0(16M), blog.1(32M)...
    • MongoDB preallocates data files to ensure consistent performance
  • Memory Management
    • Memory-mapped storage engine
      • Maps all it's data files in memory
      • The virtual size of mongod process may very large
    • Leave other works to operating system
    • 32bit MongoDB server limit to 2G data per mongod instance
  • 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
  • Getting Started
  • Installation
    • Very easy, just download, and extract
    • 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
  • Shell Client
    • ./mongo [localhost:27017/test]
    • use blog
    • db.posts.insert({“title”:”Hello, MongoDB”})
    • db.posts.find()
    • db.posts.remove([criteria])
    • db.posts.drop()
    • ...
  • PHP driver
    • pecl install mongo
    • Or dowload and compile
    • Extenstion address: http://pecl.php.net/package/mongo
    • Enable at php.ini
      • extension=mongo.so
  • CRUD
  • Before CRUD
    • Create Database?
    • Create Collection(table)?
    • Alter Schema?
    NO
  • 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;
  • Creating
  • 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'));
  • Querying
    • Query can be complex
      • $lt, $lte, $gt, $gte, $not, $in...
      • $where, array('$where'=>'function(){... return true/false}')
      • Regular expression, array('title'=>'/^Hello/i')
      • Limit/Sort with cursor
      • Aggregation, count/group
    • Reference
      • http://www.mongodb.org/display/DOCS/Querying
      • http://www.mongodb.org/display/DOCS/Advanced+Queries
  • Updating
  • Updating Using updating modifier: $inc, $set, $push, ...
  • Why Modifiers
    • Convenient
    • Atomically – instead of locking and transactions
    • Reference
      • http://www.mongodb.org/display/DOCS/Atomic+Operations
      • http://www.mongodb.org/display/DOCS/Updating
  • Upserts Like REPLACE statement in MySQL
  • Deleting
    • $collection->remove(/*criteria*/)
    • $collection->drop()
  • Advanced Features
  • MapReduce
    • Aggregation parallelized across multiple servers
    • Count, distinct, group can be done with it
    • Speed is the cost, not use in “real time”
  • Replication
    • Master-slave, slave automatically synchronize the data
      • ./mongod --dbpath ~/dbs/master --port 27017 --master
      • ./mongod --dbpath ~/dbs/slave --port 10001 --slave --source localhost:27017
    • Replica Sets
      • Master-slave, but auto-failover
      • Not only one master
      • mongod --port 27017 --dbpath /data/node1 --replSet cluster/localhost:10001
      • mongod --port 10001 --dbpath /data/node1 --replSet cluster/localhost:27017
      • db.runCommand({“replSetInitiate”:{“_id”:”cluster”, “members”:[{“_id”:1,”host”:”locahost:27017”},...]}})
  • Sharding
    • Store different data in different servers
    • Sharding key, one key of a collection used to split data
    • A shard is can be a single server or a replica set
  • 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
    • Old document will be replaced if collection if full
    • High performance, auto-FIFO, age-out
      • Loging, caching, ...
  • GridFS
    • Store large binary files
    • Replication, auto-sharding
      • Failover, scale-out
    • Use mongofiles
      • ./mongofiles [put|get|list|...] file.png
    • PHP: MongoGridFS, MongoGridFSFile, MongoGridFSCursor
  • The End http://fwso.cn