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

mongodb_web_application_-_MongoDC_2011.ppt.pdf

 mongodb_web_application_-_MongoDC_2011.ppt.pdf

mongodb

July 11, 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. 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
  3. Documents doc1 = { _id: 4b97e62bf1d8c7152c9ccb74, key1: value1, 
 key2:

    value2, key3: {..., ..., ...},
 key4: [..., ..., ]
 }
  4. place1 = { name: "10gen HQ”, 
 address: ”134 5th

    Avenue 3rd Floor”,
 city: "New York”,
 zip: "10011”
 } db.places.find({zip:”10011”}).limit(10) Places v1
  5. place1 = { name: "10gen HQ”, 
 address: "17 West

    18th Street 8th Floor”,
 city: "New York”,
 zip: "10011”, tags: [“business”, “recommended”]
 } db.places.find({zip:”10011”, tags:”business”}) Places v2
  6. place1 = { name: "10gen HQ”, 
 address: "17 West

    18th Street 8th Floor”,
 city: "New York”,
 zip: "10011”, tags: [“business”, “cool place”], latlong: [40.0,72.0]
 } db.places.ensureIndex({latlong:”2d”}) db.places.find({latlong:{$near:[40,70]}}) Places v3
  7. place1 = { name: "10gen HQ”, 
 address: "17 West

    18th Street 8th Floor”,
 city: "New York”,
 zip: "10011”,
 latlong: [40.0,72.0], tags: [“business”, “cool place”],
 tips: [ {user:"nosh", time:6/26/2010, tip:"stop by for office hours on Wednesdays from 4-6pm"}, {.....}, {.....} ]
 } Places v4
  8. 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: “business”}) Querying your Places
  9. Initial data load: db.places.insert(place1) Updating 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"}}}}
  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. user1 = { name: “nosh” email: “[email protected]”, . . .

    checkins: [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab]
 } checkins [] = ObjectId reference to checkin collection Users
  12. checkin1 = { place: “10gen HQ”, ts: 6/7/2011 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
  13. 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
  14. 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}})
  15. 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
  16. Single Master Deployments Primary/Master Secondary/Slave • Configure as a replica set

    for automated failover • Add more secondaries to scale reads
  17. 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
  18. 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