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

MongoDB Philly 2012: Welcome and Building Your ...

mongodb
April 10, 2012
410

MongoDB Philly 2012: Welcome and Building Your First Application with MongoDB

MongoDB – from "humongous" – is an open source, non-relational, document-oriented database. Trading off a few traditional features of databases (notably joins and transactions) in order to achieve much better performance, MongoDB is fast, scalable, and designed for web development. The goal of the MongoDB project is to bridge the gap between key-value stores (which are fast and highly scalable) and traditional RDBMS systems (which provide rich queries and deep functionality). To elaborate on these features we will go through building an application with MongoDB from start to finish.
The presentation will be accessible to those who are completely new to MongoDB and non-relational databases, but will touch on some of the advanced functionality that makes MongoDB a compelling option for all web developers.

mongodb

April 10, 2012
Tweet

Transcript

  1. Agenda •MongoDB: Data modeling, queries, geospatial, updates, map reduce •Using

    a location-based app as an example •Example Works in MongoDB JS shell Tuesday, April 10, 12
  2. Why MongoDB •Open source •Designed for today •Today’s hardware /

    environments •Today’s challenges •Great developer experience •Reliable •Scalable Tuesday, April 10, 12
  3. Use Cases Web 2.0, Media, SaaS, Gaming, Finance, Telecom, Healthcare

    •RDBMS replacement for high-traffic web applications •Content Management-type applications •Real-time analytics •High-speed data logging Tuesday, April 10, 12
  4. Let’s Design An App •Users Check-in to a Location •Leave

    Notes and Comments About That Location •Requirements, then Documents Tuesday, April 10, 12
  5. Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ JSON

    Document Index ➜ Index Join ➜ Embedded Document Partition ➜ Shard Partition Key ➜ Shard Key Tuesday, April 10, 12
  6. Documents Note: _id is unique, but can be anything you’d

    like { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "spf13", date : "Sat Jul 24 2010 19:47:11", text : "MongoSF", tags : [ "San Francisco", "MongoDB" ] } Tuesday, April 10, 12
  7. Collections Places Users Check-Ins Doc 4 Doc 5 Doc 6

    Doc 7 Doc 8 Doc 9 Doc 1 Doc 2 Doc 3 Tuesday, April 10, 12
  8. Application Goals Places Check ins (1) Q: Current location A:

    Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins Tuesday, April 10, 12
  9. Places v1 place1 = { name : "10gen HQ", address

    : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011" } Tuesday, April 10, 12
  10. Places v2 place1 = { name : "10gen HQ", address

    : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011" } > db.posts.find({zip: "10011"}).limit(10) Tuesday, April 10, 12
  11. Places v2 place1 = { name : "10gen HQ", address

    : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011", tags : [ "business", "awesome" ] } Tuesday, April 10, 12
  12. Places v3 place1 = { name : "10gen HQ", address

    : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011", tags : [ "business", "awesome" ] } > db.posts.findOne({ zip: "10011", tags: "awesome" }) Tuesday, April 10, 12
  13. Places v3 place1 = { name : "10gen HQ", address

    : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011", tags : [ "business", "awesome" ], latlong : [40.0,72.0] } Tuesday, April 10, 12
  14. Places v3 place1 = { name : "10gen HQ", address

    : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011", tags : [ "business", "awesome" ], latlong : [40.0,72.0] } > db.places.ensureIndex({latlong : "2d"}) Tuesday, April 10, 12
  15. Places v3 place1 = { name : "10gen HQ", address

    : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011", tags : [ "business", "awesome" ], latlong : [40.0,72.0] } > db.places.ensureIndex({latlong : "2d"}) > db.places.find({latlong:{$near:[40,70]}}) Tuesday, April 10, 12
  16. Places v4 place1 = { name : "10gen HQ", address

    : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011", tags : [ "business", "awesome" ], latlong : [40.0,72.0], tips : [ { user : "ryan", time : 6/26/2010, tip : "stop by for office hours on Wednesdays from 4-6pm"}, {.....}] } Tuesday, April 10, 12
  17. Querying Your Place Creating Your Index > db.places.ensureIndex({latlong : "2d"})

    > db.places.ensureIndex({ tags : 1 }) > db.places.ensureIndex({ name : 1 }) Tuesday, April 10, 12
  18. Querying Places Finding Places With Regular Expressions By Tag >

    db.places.find({latlong:{$near:[40,70]}}) > db.places.find({name: /^typeAheadString/ }) > db.posts.find({tags: "business" }) Tuesday, April 10, 12
  19. Application Goals Places Check ins (1) Q: Current location A:

    Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins Tuesday, April 10, 12
  20. Updating Places Using Update to Add Tips > db.places.update({name:"10gen HQ"},

    { $push : {tips: {user:"nosh", time: 6/26/2010, tip:"stop by for office hours on Wednesdays from 4-6"}}}}) Tuesday, April 10, 12
  21. Atomic Updates • $set, $unset, $rename • $push, $pop, $pull,

    $addToSet • $inc Tuesday, April 10, 12
  22. Application Goals Places Check ins (1) Q: Current location A:

    Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins Tuesday, April 10, 12
  23. Users checkins [] = ObjectId reference to check-in collection user1

    = { name : "ben", address : "[email protected]", ... checkins : [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab] } Tuesday, April 10, 12
  24. Check-Ins checkin1 = { place : "10gen HQ", ts :

    9/20/2010 10:12:00, userId : <objectId of user> } Tuesday, April 10, 12
  25. Checking In •Insert check in object [checkin collection] •Update ($push)

    user object [user collection] 2 Operations Tuesday, April 10, 12
  26. Application Goals Places Check ins (1) Q: Current location A:

    Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins Tuesday, April 10, 12
  27. Simple Stats > db.checkins.find({place : "10gen HQ"}) > db.checkins.find({place :

    "10gen HQ"}).sort({ts:-1}).limit(10) > db.checkins.find({place : "10gen HQ", ts: { $gt: <midnight>}}).limit() > db.checkins.find().sort({ts:-1}).limit(50) Tuesday, April 10, 12
  28. Stats With MapReduce mapFunc = function() { emit(this.place, 1); }

    reduceFunc = function(key, values) { return Array.sum(values); } db.checkins.mapReduce(mapFunc,reduceFunc, { query: { timestamp: {$gt:<nowminus3hrs>}}, out: result }) result = [{_id:”10gen HQ”, value: 17}, {…..}, {….}] db.result.find({ value: {$gt: 15}, _id: {$in: [….., ….., …..]} }) Tuesday, April 10, 12
  29. Application Goals Places Check ins (1) Q: Current location A:

    Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins : ) Tuesday, April 10, 12
  30. App Server App Server App Server Sharding MongoD MongoD MongoD

    MongoD MongoD MongoD MongoD MongoD MongoD MongoD MongoD MongoD ConfigD ConfigD ConfigD MongoS MongoS MongoS Tuesday, April 10, 12