MongoDB & PHP

4,220 views
3,221 views

Published on

A Brief Intro

Published in: Technology
2 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total views
4,220
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
79
Comments
2
Likes
3
Embeds 0
No embeds

No notes for slide

MongoDB & PHP

  1. + 1
  2. I am Sanjeev Shrestha @sanjeevshrestha 2
  3. In these slides... NoSQL TYPES MongoDB INSTALLATIONPHP & MongoDB 3
  4. Intended AudienceBEGINNERS 4
  5. 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
  6. DATA is Unstructured Does not make sense OWDATA is GR IN G at high speed 6
  7. 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
  8. NoSQL Storage TypesDOCUMENT STORE GRAPHKEY-VALUE STORE TABULAR multivalue database object database rdf database tuple store 8
  9. 9
  10. MongoDB High PERFORMANCE CONSISTENT FullyHORIZONTALLY scalable DOUMENT Oriented 10
  11. Best Features of KEY-VALUE stores,DOCUMENT database & RELATIONAL databases 11
  12. 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
  13. Built for SPEED Built for SPEED (written in C++) Data serialized to BSONExtensive use of MEMORY MAPPED Files PADS DISK SPACE around document 13
  14. Why MongoDB?HIGH AVAILABILITY JOURNALING REPLICATION SHARDING INDEXINGAGGREGATION MAP/REDUCE 14
  15. 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. 16
  17. MONGODB seems to be the leader. CASSANDRA stands close second. 17
  18. RDBMS VS MONGODBSTRUCTURERDBMS MONGODBDATABASE DATABASETABLES COLLECTIONSCOLUMNS DOCUMENTSROWS FIELDS 18
  19. RDBMS VS MONGODBRDBMS MONGODBSTORED PROCEDURES STORED JAVASCRIPTDATABASE SCHEMA SCHEMA FREESUPPORTS JOINS NO JOINS 19
  20. Relational normalized DATA 20
  21. Document Database normalized DATA 21
  22. MySQL to MongoDBMySQL Term MongoDB TermDatabase DatabaseTables CollectionRows BSON DocumentColumn BSON FieldsIndex IndexJoin Embedding and LinkingPrimary Key _id fieldGroup by Aggregation 22
  23. 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
  24. 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
  25. 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
  26. INSTALLATION It is DEAD SIMPLE Refer to the following linkhttp://www.mongodb.org/display/DOCS/Quickstart/ 26
  27. Get started with MongoDBMONGO SHELL$ mongo 27
  28. Listing DATABASESshow dbs; 28
  29. Creating DATABASEuse databasenamebut DB is not createdDB is created only when youcreate a collection 29
  30. Creating a COLLECTIONdb.createCollection(articles)No Definition required 30
  31. Listing COLLECTIONSshow collections 31
  32. 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
  33. Listing DOCUMENTSdb.collection.find() 33
  34. 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
  35. creating INDEXdb.collection.ensureIndex(index);eg.db.articles.ensureIndex({title:1}) 35
  36. MONGODB-PHP EXTENSION Available from PECLshell> pecl install mongo 36
  37. 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
  38. Is it LOADED?Check PHP information 38
  39. Creating a CONNECTION 39
  40. Selecting DB & COLLECTION 40
  41. Listing from a COLLECTION 41
  42. Adding DOCUMENT 42
  43. 43
  44. Updating a DOCUMENT 44
  45. 45
  46. Deleting a DOCUMENT 46
  47. Working with multiple COLLECTIONS no JOINS Instead REFs are used TWO TypesMANUAL / DBREFs 47
  48. Manual REFs 48
  49. DBREFs 49
  50. Listing Data from multiple COLLECTION 50
  51. What NEXT?GRIDFSMAP/REDUCEMONGOCODE {stored javascript}AGGREGATIONSHARDINGREPLICAS 51
  52. REFERENCEShttp://php.net/mongo/http://www.mongodb.org/display/DOCS/PHP+Language+Center 52
  53. THANK YOU &HAPPY CODING 53

×