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

An Evening with MongoDB Toronto 2012 - Building Applications with MongoDB

D8fc2580cfaca035f666d9e4ee79a7f7?s=47 mongodb
August 02, 2012
390

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.

D8fc2580cfaca035f666d9e4ee79a7f7?s=128

mongodb

August 02, 2012
Tweet

Transcript

  1. An Introduction With Randall Hunt Building Applications With MongoDB @jrhunt

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

    a location-based app as an example Example Works in MongoDB JS shell • • •
  3. Why MongoDB Open source Designed for today Today’s hardware /

    environments Today’s challenges Great developer experience Reliable • • • • • •
  4. 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 • • • •
  5. In Good Company

  6. Let’s Design An App Users Check-in to a Location Leave

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

    Document Index ➜ Index Join ➜ Embedded Document Partition ➜ Shard Partition Key ➜ Shard Key
  8. 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" ] }
  9. Collections Places Users Check-Ins Doc 4 Doc 5 Doc 6

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

    address : "578 Broadway 7th Floor", city : "New York", zip : "10012" }
  12. Inserting Places Initial Data Load > db.places.insert(place1)

  13. 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" }
  14. Places v2 place1 = { name : "10gen HQ", address

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

    "business", "awesome" ] }); > db.places.find({_id: ObjectId(...)}) { _id : ObjectId(...) tags : [ "business", "awesome" ] }
  16. Updating A Collection > db.places.update({zip: "10012"}, { $set : {

    tags : [ "business", "awesome" ] } } )
  17. 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" })
  18. $push > db.posts.update( { _id: ObjectId(...)}, { $push: {tags: "moreawesome"}}

    )
  19. .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" : { } }
  20. 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", ....
  21. GEO Features place1 = { name : "10gen HQ", address

    : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ], latlong : [40.0,72.0] }
  22. 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"})
  23. 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]}})
  24. 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" })
  25. 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"}, {.....}] }
  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
  27. 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"}}}})
  28. Atomic Updates $set, $unset, $rename $push, $pop, $pull, $addToSet $inc

    • • •
  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
  30. Users checkins [] = ObjectId reference to check-in collection user1

    = { name : "Randall", address : "randall@10gen.com", ... checkins : [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab] }
  31. Check-Ins checkin1 = { place : "10gen HQ", ts :

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

    user object [user collection] • • 2 Operations
  33. Querying Your Place Creating Your Index > db.checkins.ensureIndex({place:1,ts:1}) > db.checkins.ensureIndex({

    ts:1 })
  34. 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
  35. 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)
  36. 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: [….., ….., …..]} })
  37. 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 : )
  38. MongoDB Deployments

  39. Replica Sets Primary Secondary Secondary Primary Secondary Arbiter Primary Secondary

    Secondary Secondary Secondary
  40. 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
  41. Try it for yourself at try.mongodb.org

  42. ranman.com github.com/ranman @jrhunt We’re hiring!! Engineers, Sales, Evangelist, Marketing &

    Support Contact us at jobs@10gen.com Questions? download at mongodb.org