Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to MongoDB

Alex Bilbie
February 19, 2012

Introduction to MongoDB

Presented at CodeIgniter conference UK in February 2011 - http://ciconf.com/uk/2012

Alex Bilbie

February 19, 2012
Tweet

More Decks by Alex Bilbie

Other Decks in Technology

Transcript

  1. MongoDB Developed by 10gen in 2007 Open sourced in 2009

    Bridge the gap between key-value stores and RDBMS
  2. MongoDB Performance “Bitchin’ fast” Rich dynamic queries Lazy creation Schema-less

    Easy replication and failover Auto sharding 13 official language drivers (dozens of community-created drivers)
  3. { “_id” : ObjectID(“7qhlhsdf89sdf98899989a”), “name” : “Alex”, “age” : 22,

    “tags” : [“PHP”, “REST”, “APIs”, “MongoDB”] }
  4. Documents Can store JSON types - strings, integers, floats, doubles,

    arrays, objects, null, boolean Special types - data, object id, binary, regex, and code 16mb hard limit Every document can have different keys
  5. PHP driver pecl  install  mongo Source on Github - http://lncn.eu/cdu7

    Regularly updated CodeIgniter library - http://lncn.eu/fmy5
  6. >  show  dbs    admin    blog >  use  blog

       switched  to  blog >  show  collections    posts
  7. >  db.posts.findOne()    {        _id  :  ObjectId(‘8dfhosiahdf89sf9sd’),

           title  :  “Hello  world!”,        body  :  “Lorem  ipsum...”,        author  :  {            name  :  “Alex”        }    } >  db.posts.find().limit(1) >  db.posts.find().limit(1).pretty()
  8. >  db.posts.insert({        title  :  “Another  post”,  

         body  :  “Dolor  sit  amet...”        author  :  {            name  :  “Alex”        },        tags  :  [“PHP”,  “MongoDB”]    }) >  db.posts.save({...})
  9. SELECT  *  FROM  posts  WHERE  title  =   “Another  Post”

    db.posts.find({title  :  “Another   Post”}) {  _id  :   ObjectId(‘8dfhosiahdf89sf9sd’),   title  :  “Another  post”,  body  :   “Dolor  sit  amet...”,  author  :   {name  :  “Alex”},  tags  :  [“PHP”,   “MongoDB”]  }
  10. SELECT  body  FROM  posts  WHERE  title   =  “Another  Post”

    db.posts.find({title  :  “Another   Post”},  {body  :  1}) {  _id  :   ObjectId(‘8dfhosiahdf89sf9sd’),     body  :  “Dolor  sit  amet...”  }
  11. db.people.insert({name  :  “Alex”,  age:  22,   sex  :  “Male”}) db.people.insert({name

     :  “Nick”,  age:  24,   sex  :  “Male”}) db.people.insert({name  :  “Steph”,  age:  28,   sex  :  “Female”})
  12. $gt        Greater  than $gte      Greater

     than  or  equal  to $lt        Less  than $lte      Less  than  or  equal  to $ne        Not  equal  to $in        In  array $nin      Not  in  array
  13. $mod      Mod  operator $all      Matches  all

     values  in  array $size    Size  of  array $exists    Key  in  array  exists $type    Matches  data  type $not      Negates  value  of  another  operator $or        Where  ==  OR  == $nor      Where  !==  AND  !==
  14. {    _id:  ObjectId(‘...’),    name:  “Hotel  IBIS”    location:

     {        lon:  54.2285,        lat:  -­‐0.5477    } } db.places.ensureIndex({location:  2d})
  15. $inc              Increment  value $set

                 Set  field  to  value $unset          Delete  field $push            Appends  field  to  value  (if  field   is  an  array  otherwise  works  like  $set) $pushAll      Multiple  $push $addToSet    $push  only  if  not  exists $pop              Array  pop $pull            Removes  all  occurrences  of  value
  16. $pull          Removes  all  occurrences   of

     value $pullAll    Pull  all  values $rename      Rename  field $bit            Bitwise  update  of  field
  17. Embed {    comments:  [        {  

             text:  “Awesome  dude!”        },        {            text:  “Totally  radical”        }    ] }
  18. Double query + Don’t need to worry about hard limit

    + Much easier querying +/- Double query
  19. Some untruths “MongoDB is not single server durable” “MongoDB will

    lose my data because it is not ACID compliant” “I need 12 terabytes of RAM to use MongoDB” “MongoDB is just CouchDB but with more marketing weight behind it”
  20. Appropriate Use Caes Logs Data warehousing / archiving Location based

    apps (Foursquare / o2 Priorities) Ecommerce (with RDBMS for billing) Gaming Real time stats
  21. CodeIgniter and MongoDB CodeIgniter library - http://lncn.eu/fmy5 Follows the query

    builder (active record) database library Version 2.0 almost finished
  22. CodeIgniter and Mongo $this-­‐>mongo_db    -­‐>select(array(‘name’,  ‘age’))    -­‐>where_lte(‘age’,  25)

       -­‐>where_in(‘interests’,   array(‘PHP,  ‘MongoDB’))    -­‐>get(‘people’);
  23. CodeIgniter Library v2.0 Multiple database support Epic code clean up

    Remove CodeIgniter-only functions so can be used in other frameworks or vanilla PHP Supports new MongoDB 2.0+ features