Slide 1

Slide 1 text

MongoDB Alex Bilbie

Slide 2

Slide 2 text

Alex Bilbie Developer at the University of Lincoln Happy MongoDB user since 2010 Tweet me @alexbilbie

Slide 3

Slide 3 text

Relational Database Management Systems

Slide 4

Slide 4 text

Relational Database Management Systems (RDBMS)

Slide 5

Slide 5 text

RDBMS Almost every application has one Schemas Joins, aggregation, normalisation

Slide 6

Slide 6 text

RDBMS Difficult to scale Can be inflexible

Slide 7

Slide 7 text

MongoDB Developed by 10gen in 2007 Open sourced in 2009 Bridge the gap between key-value stores and RDBMS

Slide 8

Slide 8 text

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)

Slide 9

Slide 9 text

Functionality Performance RDBMS MongoDB Memcached Key/value stores

Slide 10

Slide 10 text

Limitations No transactions No joins 32bit integers Memory-whore 16MB document size Authentication

Slide 11

Slide 11 text

Some terminology Document == row Collection == table Database == database

Slide 12

Slide 12 text

Documents

Slide 13

Slide 13 text

{ “_id” : ObjectID(“7qhlhsdf89sdf98899989a”), “name” : “Alex”, “age” : 22, “tags” : [“PHP”, “REST”, “APIs”, “MongoDB”] }

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Collections

Slide 16

Slide 16 text

Collections Logical groups of documents Indexes

Slide 17

Slide 17 text

Databases Contain collections Authentication Global read/write user Local read/write user Local read only user

Slide 18

Slide 18 text

Executables mongo mongod mongos mongodump mongorestore mongosniff

Slide 19

Slide 19 text

PHP driver pecl  install  mongo Source on Github - http://lncn.eu/cdu7 Regularly updated CodeIgniter library - http://lncn.eu/fmy5

Slide 20

Slide 20 text

mongo

Slide 21

Slide 21 text

>  show  dbs    admin    blog >  use  blog    switched  to  blog >  show  collections    posts

Slide 22

Slide 22 text

>  db.posts.count()    1

Slide 23

Slide 23 text

>  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()

Slide 24

Slide 24 text

>  db.posts.insert({        title  :  “Another  post”,        body  :  “Dolor  sit  amet...”        author  :  {            name  :  “Alex”        },        tags  :  [“PHP”,  “MongoDB”]    }) >  db.posts.save({...})

Slide 25

Slide 25 text

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”]  }

Slide 26

Slide 26 text

SELECT  body  FROM  posts  WHERE  title   =  “Another  Post” db.posts.find({title  :  “Another   Post”},  {body  :  1}) {  _id  :   ObjectId(‘8dfhosiahdf89sf9sd’),     body  :  “Dolor  sit  amet...”  }

Slide 27

Slide 27 text

db.posts.find({},  {author.name  :  1})

Slide 28

Slide 28 text

db.posts.find({author.name  :  /Alex/},   {author.name  :  1}) db.posts.find({author.name  :  /alex/i},   {author.name  :  1})

Slide 29

Slide 29 text

db.posts.find().sort(title  :  1) db.posts.find().sort(title  :  -­‐1) db.posts.find().limit(1) db.posts.find().limit(1).skip(1)

Slide 30

Slide 30 text

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”})

Slide 31

Slide 31 text

SELECT  *  FROM  people  WHERE  age  >  22 db.people.find({age  :  {$gt  :  22}})

Slide 32

Slide 32 text

SELECT  *  FROM  people  WHERE  age  <=  25 db.people.find({age  :  {$lte  :  25}})

Slide 33

Slide 33 text

$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

Slide 34

Slide 34 text

$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  !==

Slide 35

Slide 35 text

//  single  ascending  index db.people.ensureIndex({name:1})

Slide 36

Slide 36 text

//  single  descending  index db.people.ensureIndex({name:-­‐1})

Slide 37

Slide 37 text

//  unique db.people.ensureIndex({name:-­‐1},   {unique:  true})

Slide 38

Slide 38 text

//  non  blocking db.people.ensureIndex({name:1},   {background:true})

Slide 39

Slide 39 text

//  compound db.people.ensureIndex({name:1,  age: 1})

Slide 40

Slide 40 text

{    _id:  ObjectId(‘...’),    name:  “Hotel  IBIS”    location:  {        lon:  54.2285,        lat:  -­‐0.5477    } } db.places.ensureIndex({location:  2d})

Slide 41

Slide 41 text

//  Remove  all  documents db.people.remove() //  Remove  with  condition db.people.remove({name:  “Alex”})

Slide 42

Slide 42 text

//  Update db.people.update({name:  “Alex”},   {$set  :  {name:  “Alex  Bilbie”})

Slide 43

Slide 43 text

$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

Slide 44

Slide 44 text

$pull          Removes  all  occurrences   of  value $pullAll    Pull  all  values $rename      Rename  field $bit            Bitwise  update  of  field

Slide 45

Slide 45 text

db.people.count() db.people.count({name:  “Alex”})

Slide 46

Slide 46 text

db.people.distinct(‘name’) db.people.distinct(‘name’,  {age:   {  $lte:  25}})

Slide 47

Slide 47 text

1. Embed 2. Separate collection + double query No joins?

Slide 48

Slide 48 text

Embed {    comments:  [        {            text:  “Awesome  dude!”        },        {            text:  “Totally  radical”        }    ] }

Slide 49

Slide 49 text

Embed + Keeps everything together (pre-joining) - Harder to query 16mb hard limit on document size

Slide 50

Slide 50 text

Double query >  db.post.find({id:  123}) >  db.comments.find({post_id:  123})

Slide 51

Slide 51 text

Double query + Don’t need to worry about hard limit + Much easier querying +/- Double query

Slide 52

Slide 52 text

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”

Slide 53

Slide 53 text

How we use MongoDB AD BP Estates Blackboard CMIS Nucleus

Slide 54

Slide 54 text

How we use MongoDB Nucleus .xml .json .csv .rdfxml .tutle .n3 .ntriples .kml

Slide 55

Slide 55 text

Appropriate Use Caes Logs Data warehousing / archiving Location based apps (Foursquare / o2 Priorities) Ecommerce (with RDBMS for billing) Gaming Real time stats

Slide 56

Slide 56 text

Less well suited use cases Tractional systems Epic join based query based systems

Slide 57

Slide 57 text

CodeIgniter and MongoDB CodeIgniter library - http://lncn.eu/fmy5 Follows the query builder (active record) database library Version 2.0 almost finished

Slide 58

Slide 58 text

CodeIgniter and Mongo $this-­‐>mongo_db    -­‐>select(array(‘name’,  ‘age’))    -­‐>where_lte(‘age’,  25)    -­‐>where_in(‘interests’,   array(‘PHP,  ‘MongoDB’))    -­‐>get(‘people’);

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

Where can I find out more? mongodb.com groups.google.com/group/mongodb-user irc://irc.freenode.net/#mongodb blog.boxedice.com cookbook.mongodb.com 10gen.com/events

Slide 61

Slide 61 text

Thanks