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

An Evening with MongoDB Toronto 2012 - Building...

mongodb
August 02, 2012
470

An Evening with MongoDB Toronto 2012 - Building Applications with MongoDB

Building Your First MongoDB Application
Randall Hunt
This talk will introduce the features of MongoDB by walking through how one can building a simple location-based checkin application using MongoDB. The talk will cover the basics of MongoDB's document model, query language, map-reduce framework and deployment architecture.
Speaker Bio: Randall is a HackNY mentor. He loves python, programming, space, and exploring NYC.

mongodb

August 02, 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 • • •
  2. Why MongoDB Open source Designed for today Today’s hardware /

    environments Today’s challenges Great developer experience Reliable • • • • • •
  3. Use Cases Web, Media, SaaS, Gaming, Finance, Telecom, Healthcare, LHC

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

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

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

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

    Doc 7 Doc 8 Doc 9 Doc 1 Doc 2 Doc 3
  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
  9. Creating A Document place1 = { name : "10gen HQ",

    address : "578 Broadway 7th Floor", city : "New York", zip : "10012" }
  10. Query A Collection > db.places.find({zip: "10012"}).limit(10) { _id : ObjectId("01aed46179d968fc55c7232")

    name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012" }
  11. Places v2 place1 = { name : "10gen HQ", address

    : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ] }
  12. Update A Collection > db.places.update({zip: "10012"}, { tags : [

    "business", "awesome" ] }); > db.places.find({_id: ObjectId(...)}) { _id : ObjectId(...) tags : [ "business", "awesome" ] }
  13. Places v2 place1 = { name : "10gen HQ", address

    : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ] } > db.posts.findOne({ zip: "10012", tags: "awesome" })
  14. .explain() > db.places.find({zip: "10012"}).explain() { "cursor" : "BasicCursor", "nscanned" :

    2, "nscannedObjects" : 2, "n" : 1, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : false, "indexOnly" : false, "indexBounds" : { } }
  15. create index > db.places.ensureIndex({name: 1}) > db.places.find({name: 1}).explain() ... "cursor"

    : "BtreeCursor name_1", ... > db.places.ensureIndex({name: 1, zip: 1}) ... "cursor" : "BtreeCursor name_1_zip_1", ....
  16. GEO Features place1 = { name : "10gen HQ", address

    : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ], latlong : [40.0,72.0] }
  17. Adding A GEO Index place1 = { name : "10gen

    HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ], latlong : [40.0,72.0] } > db.places.ensureIndex({latlong : "2d"})
  18. Places v3 place1 = { name : "10gen HQ", address

    : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ], latlong : [40.0,72.0] } > db.places.ensureIndex({latlong : "2d"}) > db.places.find({latlong:{$near:[40,70]}})
  19. Types of Queries Finding Places With Regular Expressions By Key:Value

    > db.places.find({latlong:{$near:[40,70]}}) > db.places.find({name: /^typeAheadString/ }) > db.posts.find({tags: "business" })
  20. Embedded Documents place1 = { name : "10gen HQ", address

    : "578 Broadway 7th Floor", city : "New York", zip : "10012", 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"}, {.....}] }
  21. 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
  22. Updating Places Using Update to Add Tips > db.places.update({name:"10gen HQ"},

    { $push : {tips: {user:"ranman", time:6/26/2010, tip:"stop by for office hours on Wednesdays from 4-6"}}}})
  23. 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
  24. Users checkins [] = ObjectId reference to check-in collection user1

    = { name : "Randall", address : "[email protected]", ... checkins : [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab] }
  25. Check-Ins checkin1 = { place : "10gen HQ", ts :

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

    user object [user collection] • • 2 Operations
  27. 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
  28. 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)
  29. 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: [….., ….., …..]} })
  30. 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 : )
  31. 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