MongoDB & PHP
Upcoming SlideShare
Loading in...5
×
 

MongoDB & PHP

on

  • 2,145 views

A Brief Intro

A Brief Intro

Statistics

Views

Total Views
2,145
Slideshare-icon Views on SlideShare
2,143
Embed Views
2

Actions

Likes
3
Downloads
52
Comments
2

2 Embeds 2

https://twitter.com 1
https://duckduckgo.com 1

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

12 of 2

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    MongoDB & PHP MongoDB & PHP Presentation Transcript

    • + 1
    • I am Sanjeev Shrestha @sanjeevshrestha 2
    • In these slides... NoSQL TYPES MongoDB INSTALLATIONPHP & MongoDB 3
    • Intended AudienceBEGINNERS 4
    • Challenges with RDBMSAssumesWELL DEFINED data structuredata is DENSE and largely UNIFORMPROPERTIES of data can be defined upfrontINTERRELATIONSHIP are well ESTABLISHED & SYSTEMATICINDEXES can be CONSISTENTLY DEFINEDMay SNAP when data grows enormously But ... that may not be TRUE 5
    • DATA is Unstructured Does not make sense OWDATA is GR IN G at high speed 6
    • NoSQL?No Rel / No SQL / Not Only SQL / NoSQL Non RELATIONAL DISTRIBUTED DATASTORES May NOT provide ACID Not a Single Product or Technology Data Storage and Manipulation 7
    • NoSQL Storage TypesDOCUMENT STORE GRAPHKEY-VALUE STORE TABULAR multivalue database object database rdf database tuple store 8
    • 9
    • MongoDB High PERFORMANCE CONSISTENT FullyHORIZONTALLY scalable DOUMENT Oriented 10
    • Best Features of KEY-VALUE stores,DOCUMENT database & RELATIONAL databases 11
    • Why so POPULAR? EASY to USE SIMPLER concepts EASY on ramp for NoSQLMany DEV/OPERATIONAL things come inbuilt More AGILE than RDBMSLess UPFRONT DESIGN needed than RDBMS SCALABLE schema and storage 12
    • Built for SPEED Built for SPEED (written in C++) Data serialized to BSONExtensive use of MEMORY MAPPED Files PADS DISK SPACE around document 13
    • Why MongoDB?HIGH AVAILABILITY JOURNALING REPLICATION SHARDING INDEXINGAGGREGATION MAP/REDUCE 14
    • Negatives of MONGODB INDEXES are not flexibleREALTIME QUERIES may not be as fast as others Good enough only if queries are SIMPLE Not as MATURED as RDBMS 15
    • 16
    • MONGODB seems to be the leader. CASSANDRA stands close second. 17
    • RDBMS VS MONGODBSTRUCTURERDBMS MONGODBDATABASE DATABASETABLES COLLECTIONSCOLUMNS DOCUMENTSROWS FIELDS 18
    • RDBMS VS MONGODBRDBMS MONGODBSTORED PROCEDURES STORED JAVASCRIPTDATABASE SCHEMA SCHEMA FREESUPPORTS JOINS NO JOINS 19
    • Relational normalized DATA 20
    • Document Database normalized DATA 21
    • MySQL to MongoDBMySQL Term MongoDB TermDatabase DatabaseTables CollectionRows BSON DocumentColumn BSON FieldsIndex IndexJoin Embedding and LinkingPrimary Key _id fieldGroup by Aggregation 22
    • SQL to MONGOSQL Statements Mongo StatementsCREATE DATABASE sqltest use mongotestUSE sqltest implicitCREATE TABLE table1 (a Number, b db.createCollection(coll1);Number)ALTER TABLE table1 add … implicitINSERT INTO table1 values(100,22) db.coll1.insert({a:100,b:22}); 23
    • SQL to MONGOSQL Statements Mongo StatementsSELECT * from table1 db.coll1.find();SELECT * from table1 where a=33 db.coll1.find({a:33});SELECT a,b from table1 where a =44 db.coll1.find({a:44},{a:1,b:1});SELECT * from table1 where b like %abc% db.coll1.find({b:/abc/});SELECT * from table1 where b like abc% db.coll1.find({b:/^abc/});SELECT * from table1 where a>50 db.coll1.find({a:{$gt:50}});SELECT * from table1 where a <40 db.coll1.find({a:{$lt:40}});SELECT * from table1 where a<=40 db.coll1.find({a:{$lte:40}});SELECT * from table1 where a =33 order by db.coll1.find({a:33}).sort({name:1})nameSELECT * from table1 where a=33 order by db.coll1.find({a:33}).sort({name:-1})name DESCSELECT * from table1 LIMIT 1 db.coll1.findOne(); 24
    • SQL to MONGOSQL Statements Mongo StatementsSELECT * from table1 where a=33 and b=100 db.coll1.find({a:33,b:100});SELECT * from table1 LIMIT 100 db.coll1.find().limit(100);SELECT * from table1 where a =33 or a=45 or db.coll1.find({$or:[{a:33},{a:45},{a:50}]})a=50SELECT DISTINCT a from table1 db.coll1.distinct(a);SELECT count(*) from table1 db.coll1.count();SELECT count(*) from table1 where a=33 db.coll1.find({a:33}).count();CREATE INDEX myindexname ON table1(a) db.coll1.ensureIndex({a:1})UPDATE table1 set a=44 where b=100 db.coll1.update({b:100},{$set: {a:44}},false,true);DELETE from table1 where a=44 db.coll1.remove({a:44}); 25
    • INSTALLATION It is DEAD SIMPLE Refer to the following linkhttp://www.mongodb.org/display/DOCS/Quickstart/ 26
    • Get started with MongoDBMONGO SHELL$ mongo 27
    • Listing DATABASESshow dbs; 28
    • Creating DATABASEuse databasenamebut DB is not createdDB is created only when youcreate a collection 29
    • Creating a COLLECTIONdb.createCollection(articles)No Definition required 30
    • Listing COLLECTIONSshow collections 31
    • Inserting DOCUMENTdb.collection.insert(<json formatted data>);e.g.db.articles.insert({title:This is test article,slug:this-is-test-article,text:This is test articleand it does not contain anything,count:10}); 32
    • Listing DOCUMENTSdb.collection.find() 33
    • Updating DOCUMENTSdb.collection.update( criteria, objNew, upsert, multi )Upsert = update data if exists, insert as new data if notMulti = make multiple updates matching criteriae.g. db.articles.update({count:20},{$set:{title:This is second article,slug:this-is-second-art}},true,false) 34
    • creating INDEXdb.collection.ensureIndex(index);eg.db.articles.ensureIndex({title:1}) 35
    • MONGODB-PHP EXTENSION Available from PECLshell> pecl install mongo 36
    • Loading MONGO EXTENSION in PHPAdd extension=mongo.so to php.iniOr any other file like mongo.ini but make sure it is loaded by php 37
    • Is it LOADED?Check PHP information 38
    • Creating a CONNECTION 39
    • Selecting DB & COLLECTION 40
    • Listing from a COLLECTION 41
    • Adding DOCUMENT 42
    • 43
    • Updating a DOCUMENT 44
    • 45
    • Deleting a DOCUMENT 46
    • Working with multiple COLLECTIONS no JOINS Instead REFs are used TWO TypesMANUAL / DBREFs 47
    • Manual REFs 48
    • DBREFs 49
    • Listing Data from multiple COLLECTION 50
    • What NEXT?GRIDFSMAP/REDUCEMONGOCODE {stored javascript}AGGREGATIONSHARDINGREPLICAS 51
    • REFERENCEShttp://php.net/mongo/http://www.mongodb.org/display/DOCS/PHP+Language+Center 52
    • THANK YOU &HAPPY CODING 53