MongoDB Alex Bilbie
Alex BilbieDeveloper at the University of LincolnHappy MongoDB user since 2010Tweet me @alexbilbie
Relational DatabaseManagement Systems
Relational DatabaseManagement Systems        (RDBMS)
RDBMSAlmost every application has oneSchemasJoins, aggregation, normalisation
RDBMSDifficult to scaleCan be inflexible
MongoDBDeveloped by 10gen in 2007Open sourced in 2009Bridge the gap between key-value stores and RDBMS
MongoDBPerformance    “Bitchin’ fast”Rich dynamic queriesLazy creationSchema-lessEasy replication and failoverAuto shardin...
Memcached                      Key/valuePerformance                                    MongoDB                        stor...
LimitationsNo transactionsNo joins32bit integersMemory-whore16MB document sizeAuthentication
Some terminologyDocument == rowCollection == tableDatabase == database
Documents
{    “_id” : ObjectID(“7qhlhsdf89sdf98899989a”),    “name” : “Alex”,    “age” : 22,    “tags” : [“PHP”, “REST”, “APIs”, “M...
DocumentsCan store JSON types - strings, integers, floats, doubles,arrays, objects, null, booleanSpecial types - data, obje...
Collections
CollectionsLogical groups of documentsIndexes
DatabasesContain collectionsAuthentication   Global read/write user   Local read/write user   Local read only user
Executablesmongomongodmongosmongodumpmongorestoremongosniff
PHP driverpecl install mongoSource on Github - http://lncn.eu/cdu7Regularly updatedCodeIgniter library - http://lncn.eu/fmy5
mongo
> show dbs admin blog> use blog switched to blog> show collections posts
> db.posts.count() 1
> db.posts.findOne() {   _id : ObjectId(‘8dfhosiahdf89sf9sd’),   title : “Hello world!”,   body : “Lorem ipsum...”,   autho...
> db.posts.insert({   title : “Another post”,   body : “Dolor sit amet...”   author : {     name : “Alex”   },   tags : [“...
SELECT * FROM posts WHERE title =“Another Post”db.posts.find({title : “Another Post”}){ _id : ObjectId(‘8dfhosiahdf89sf9sd’...
SELECT body FROM posts WHERE title =“Another Post”db.posts.find({title : “Another Post”},{body : 1}){ _id : ObjectId(‘8dfho...
db.posts.find({}, {author.name : 1})
db.posts.find({author.name : /Alex/},{author.name : 1})db.posts.find({author.name : /alex/i},{author.name : 1})
db.posts.find().sort(title : 1)db.posts.find().sort(title : -1)db.posts.find().limit(1)db.posts.find().limit(1).skip(1)
db.people.insert({name : “Alex”, age: 22, sex :“Male”})db.people.insert({name : “Nick”, age: 24, sex :“Male”})db.people.in...
SELECT * FROM people WHERE age > 22db.people.find({age : {$gt : 22}})
SELECT * FROM people WHERE age <= 25db.people.find({age : {$lte : 25}})
$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$...
$mod Mod operator$all Matches all values in array$size Size of array$exists Key in array exists$type Matches data type$not...
// single ascending indexdb.people.ensureIndex({name:1})
// single descending indexdb.people.ensureIndex({name:-1})
// uniquedb.people.ensureIndex({name:-1},{unique: true})
// non blockingdb.people.ensureIndex({name:1},{background:true})
// compounddb.people.ensureIndex({name:1, age:1})
{    _id: ObjectId(‘...’),    name: “Hotel IBIS”    location: {      lon: 54.2285,      lat: -0.5477    }}db.places.ensure...
// Remove all documentsdb.people.remove()// Remove with conditiondb.people.remove({name: “Alex”})
// Updatedb.people.update({name: “Alex”},{$set : {name: “Alex Bilbie”})
$inc     Increment value$set     Set field to value$unset    Delete field$push     Appends field to value (if field is anarray...
$pull   Removes all occurrences ofvalue$pullAll Pull all values$rename Rename field$bit    Bitwise update of field
db.people.count()db.people.count({name: “Alex”})
db.people.distinct(‘name’)db.people.distinct(‘name’, {age: { $lte:25}})
No joins?1. Embed2. Separate collection + double query
Embed{    comments: [      {        text: “Awesome dude!”      },      {        text: “Totally radical”      }    ]}
Embed+ Keeps everything together (pre-joining)- Harder to query16mb hard limit on document size
Double query> db.post.find({id: 123})> db.comments.find({post_id: 123})
Double query+ Don’t need to worry about hard limit+ Much easier querying+/- Double query
Some untruths“MongoDB is not single server durable”“MongoDB will lose my data because it is not ACIDcompliant”“I need 12 t...
How we use MongoDB              ADBlackboard              BP             NucleusCMIS             Estates
How we use MongoDB                 .xml                 .json                 .csv                 .rdfxml  Nucleus       ...
Appropriate Use CaesLogsData warehousing / archivingLocation based apps (Foursquare / o2 Priorities)Ecommerce (with RDBMS ...
Less well suited use casesTractional systemsEpic join based query based systems
CodeIgniter and MongoDBCodeIgniter library - http://lncn.eu/fmy5Follows the query builder (active record) database library...
CodeIgniter and Mongo$this->mongo_db ->select(array(‘name’, ‘age’)) ->where_lte(‘age’, 25) ->where_in(‘interests’, array(‘...
CodeIgniter Library v2.0Multiple database supportEpic code clean upRemove CodeIgniter-only functions so can be used in oth...
Where can I find out more?mongodb.comgroups.google.com/group/mongodb-userirc://irc.freenode.net/#mongodbblog.boxedice.comco...
Thanks
Upcoming SlideShare
Loading in …5
×

Introduction to MongoDB

2,689 views
2,562 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,689
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Introduction to MongoDB

    1. 1. MongoDB Alex Bilbie
    2. 2. Alex BilbieDeveloper at the University of LincolnHappy MongoDB user since 2010Tweet me @alexbilbie
    3. 3. Relational DatabaseManagement Systems
    4. 4. Relational DatabaseManagement Systems (RDBMS)
    5. 5. RDBMSAlmost every application has oneSchemasJoins, aggregation, normalisation
    6. 6. RDBMSDifficult to scaleCan be inflexible
    7. 7. MongoDBDeveloped by 10gen in 2007Open sourced in 2009Bridge the gap between key-value stores and RDBMS
    8. 8. MongoDBPerformance “Bitchin’ fast”Rich dynamic queriesLazy creationSchema-lessEasy replication and failoverAuto sharding13 official language drivers (dozens of community-created drivers)
    9. 9. Memcached Key/valuePerformance MongoDB stores RDBMS Functionality
    10. 10. LimitationsNo transactionsNo joins32bit integersMemory-whore16MB document sizeAuthentication
    11. 11. Some terminologyDocument == rowCollection == tableDatabase == database
    12. 12. Documents
    13. 13. { “_id” : ObjectID(“7qhlhsdf89sdf98899989a”), “name” : “Alex”, “age” : 22, “tags” : [“PHP”, “REST”, “APIs”, “MongoDB”]}
    14. 14. DocumentsCan store JSON types - strings, integers, floats, doubles,arrays, objects, null, booleanSpecial types - data, object id, binary, regex, and code16mb hard limitEvery document can have different keys
    15. 15. Collections
    16. 16. CollectionsLogical groups of documentsIndexes
    17. 17. DatabasesContain collectionsAuthentication Global read/write user Local read/write user Local read only user
    18. 18. Executablesmongomongodmongosmongodumpmongorestoremongosniff
    19. 19. PHP driverpecl install mongoSource on Github - http://lncn.eu/cdu7Regularly updatedCodeIgniter library - http://lncn.eu/fmy5
    20. 20. mongo
    21. 21. > show dbs admin blog> use blog switched to blog> show collections posts
    22. 22. > db.posts.count() 1
    23. 23. > 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()
    24. 24. > db.posts.insert({ title : “Another post”, body : “Dolor sit amet...” author : { name : “Alex” }, tags : [“PHP”, “MongoDB”] })> db.posts.save({...})
    25. 25. SELECT * FROM posts WHERE title =“Another Post”db.posts.find({title : “Another Post”}){ _id : ObjectId(‘8dfhosiahdf89sf9sd’),title : “Another post”, body : “Dolor sitamet...”, author : {name : “Alex”}, tags :[“PHP”, “MongoDB”] }
    26. 26. SELECT body FROM posts WHERE title =“Another Post”db.posts.find({title : “Another Post”},{body : 1}){ _id : ObjectId(‘8dfhosiahdf89sf9sd’),body : “Dolor sit amet...” }
    27. 27. db.posts.find({}, {author.name : 1})
    28. 28. db.posts.find({author.name : /Alex/},{author.name : 1})db.posts.find({author.name : /alex/i},{author.name : 1})
    29. 29. db.posts.find().sort(title : 1)db.posts.find().sort(title : -1)db.posts.find().limit(1)db.posts.find().limit(1).skip(1)
    30. 30. 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”})
    31. 31. SELECT * FROM people WHERE age > 22db.people.find({age : {$gt : 22}})
    32. 32. SELECT * FROM people WHERE age <= 25db.people.find({age : {$lte : 25}})
    33. 33. $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
    34. 34. $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 !==
    35. 35. // single ascending indexdb.people.ensureIndex({name:1})
    36. 36. // single descending indexdb.people.ensureIndex({name:-1})
    37. 37. // uniquedb.people.ensureIndex({name:-1},{unique: true})
    38. 38. // non blockingdb.people.ensureIndex({name:1},{background:true})
    39. 39. // compounddb.people.ensureIndex({name:1, age:1})
    40. 40. { _id: ObjectId(‘...’), name: “Hotel IBIS” location: { lon: 54.2285, lat: -0.5477 }}db.places.ensureIndex({location: 2d})
    41. 41. // Remove all documentsdb.people.remove()// Remove with conditiondb.people.remove({name: “Alex”})
    42. 42. // Updatedb.people.update({name: “Alex”},{$set : {name: “Alex Bilbie”})
    43. 43. $inc Increment value$set Set field to value$unset Delete field$push Appends field to value (if field is anarray otherwise works like $set)$pushAll Multiple $push$addToSet $push only if not exists$pop Array pop$pull Removes all occurrences of value
    44. 44. $pull Removes all occurrences ofvalue$pullAll Pull all values$rename Rename field$bit Bitwise update of field
    45. 45. db.people.count()db.people.count({name: “Alex”})
    46. 46. db.people.distinct(‘name’)db.people.distinct(‘name’, {age: { $lte:25}})
    47. 47. No joins?1. Embed2. Separate collection + double query
    48. 48. Embed{ comments: [ { text: “Awesome dude!” }, { text: “Totally radical” } ]}
    49. 49. Embed+ Keeps everything together (pre-joining)- Harder to query16mb hard limit on document size
    50. 50. Double query> db.post.find({id: 123})> db.comments.find({post_id: 123})
    51. 51. Double query+ Don’t need to worry about hard limit+ Much easier querying+/- Double query
    52. 52. Some untruths“MongoDB is not single server durable”“MongoDB will lose my data because it is not ACIDcompliant”“I need 12 terabytes of RAM to use MongoDB”“MongoDB is just CouchDB but with more marketing weightbehind it”
    53. 53. How we use MongoDB ADBlackboard BP NucleusCMIS Estates
    54. 54. How we use MongoDB .xml .json .csv .rdfxml Nucleus .tutle .n3 .ntriples .kml
    55. 55. Appropriate Use CaesLogsData warehousing / archivingLocation based apps (Foursquare / o2 Priorities)Ecommerce (with RDBMS for billing)GamingReal time stats
    56. 56. Less well suited use casesTractional systemsEpic join based query based systems
    57. 57. CodeIgniter and MongoDBCodeIgniter library - http://lncn.eu/fmy5Follows the query builder (active record) database libraryVersion 2.0 almost finished
    58. 58. CodeIgniter and Mongo$this->mongo_db ->select(array(‘name’, ‘age’)) ->where_lte(‘age’, 25) ->where_in(‘interests’, array(‘PHP,‘MongoDB’)) ->get(‘people’);
    59. 59. CodeIgniter Library v2.0Multiple database supportEpic code clean upRemove CodeIgniter-only functions so can be used in otherframeworks or vanilla PHPSupports new MongoDB 2.0+ features
    60. 60. Where can I find out more?mongodb.comgroups.google.com/group/mongodb-userirc://irc.freenode.net/#mongodbblog.boxedice.comcookbook.mongodb.com10gen.com/events
    61. 61. Thanks

    ×