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

Building Web Applications with MongoDB: An Intr...

Building Web Applications with MongoDB: An Introduction

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.

mongodb

June 30, 2011
Tweet

More Decks by mongodb

Other Decks in Programming

Transcript

  1. Today`s Talk • MongoDB: Data modeling, queries, geospatial, updates, map reduce

    • Using a location-based app as an example • Example Works in MongoDB JS shell
  2. Design • Want to build an app where users can check

    in to a location • Leave notes or comments about that location • Approach: • Decide requirements, and then design documents
  3. Requirements Places Check ins (1) Q: Current location A: Places

    near location (2) Add user generated comments, content (3) Record user checkins (4) Stats about checkins
  4. Terminology RDBMS Mongo Table, View Collection Row(s) JSON Document Index

    Index Join Embedded Document Partition Shard Partition Key Shard Key
  5. Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date :

    "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : ”MongoSF", tags : [ ”San Francisco", ”MongoDB" ] } Notes: - _id is unique, but can be anything you’d like
  6. place1 = { name: "10gen HQz, 
 address: z134 5th

    Avenue 3rd Floorz,
 city: "New Yorkz,
 zip: "10011z
 } Places v1
  7. place1 = { name: "10gen HQz, 
 address: z134 5th

    Avenue 3rd Floorz,
 city: "New Yorkz,
 zip: "10011z
 } db.places.find({zip:”10011”}).limit(10) Places v1
  8. place1 = { name: "10gen HQz, 
 address: "17 West

    18th Street 8th Floorz,
 city: "New Yorkz,
 zip: "10011z, tags: [lbusinessz, lrecommendedz]
 } Places v2
  9. place1 = { name: "10gen HQz, 
 address: "17 West

    18th Street 8th Floorz,
 city: "New Yorkz,
 zip: "10011z, tags: [lbusinessz, lrecommendedz]
 } db.places.find({zip:”10011”, tags:”business”}) Places v2
  10. place1 = { name: "10gen HQz, 
 address: "17 West

    18th Street 8th Floorz,
 city: "New Yorkz,
 zip: "10011z, tags: [lbusinessz, lcool placez], latlong: [40.0,72.0]
 } Places v3
  11. place1 = { name: "10gen HQz, 
 address: "17 West

    18th Street 8th Floorz,
 city: "New Yorkz,
 zip: "10011z, tags: [lbusinessz, lcool placez], latlong: [40.0,72.0]
 } db.places.ensureIndex({latlong:”2d”}) Places v3
  12. place1 = { name: "10gen HQz, 
 address: "17 West

    18th Street 8th Floorz,
 city: "New Yorkz,
 zip: "10011z, tags: [lbusinessz, lcool placez], latlong: [40.0,72.0]
 } db.places.ensureIndex({latlong:”2d”}) db.places.find({latlong:{$near:[40,70]}}) Places v3
  13. place1 = { name: "10gen HQz, 
 address: "17 West

    18th Street 8th Floorz,
 city: "New Yorkz,
 zip: "10011z,
 latlong: [40.0,72.0], tags: [lbusinessz, lcool placez],
 tips: [ {user:"nosh", time:6/26/2010, tip:"stop by for office hours on Wednesdays from 4-6pm"}, {.....}, ]
 } Places v4
  14. Creating your indexes db.places.ensureIndex({tags:1}) db.places.ensureIndex({name:1}) db.places.ensureIndex({latlong:”2d”}) Finding places: db.places.find({latlong:{$near:[40,70]}}) With

    regular expressions: db.places.find({name: /^typeaheadstring/) By tag: db.places.find({tags: lbusinessz}) Querying your Places
  15. Initial data load: db.places.insert(place1) 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"}}}}
  16. 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
  17. user1 = { name: lnoshz email: [email protected], . . .

    checkins: [{ place: l10gen HQz, ts: 9/20/2010 10:12:00, userId: <objectid of user>}, … ]
 } checkins [] = ObjectId reference to checkin collection Users
  18. user1 = { name: lnoshz email: [email protected], . . .

    checkins: [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab]
 } checkins [] = ObjectId reference to checkin collection Alternative
  19. checkin1 = { place: l10gen HQz, ts: 9/20/2010 10:12:00, userId:

    <objectid of user>
 } Check-in = 2 ops Insert check in object [checkin collection] Update ($push) user object [user collection] Indexes: db.checkins.ensureIndex({place:1, ts:1}) db.checkins.ensureIndex({ts:1}) Checkins
  20. 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: lresultz}) [{_id:z10gen HQz, value: 17}, {…..}, {….}] result.find({ value: {$gt: 15}, _id: {$in: [….., ….., …..]} })
  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. Single Master Deployments Primary/Master Secondary/Slave • Configure as a replica set

    for automated failover • Add more secondaries to scale reads
  23. Auto Sharded Deployment Primary/Master Secondary/Slave MongoS • Autosharding distributes data among

    two or more replica sets • Mongo Config Server(s) handles distribution & balancing • Transparent to applications Mongo Config
  24. Use Cases • RDBMS replacement for high-traffic web applications • Content Management-type

    applications • Real-time analytics • High-speed data logging Web 2.0, Media, SaaS, Gaming, Finance, Telecom, Healthcare