Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Building your first application w/mongoDB MongoSV2011

3.794 Aufrufe

Veröffentlicht am

This talk will introduce the features of MongoDB by walking through how one can building a simple location-based application using MongoDB. The talk will cover the basics of MongoDB's document model, query language, map-reduce framework and deployment architecture.

Veröffentlicht in: Technologie

Building your first application w/mongoDB MongoSV2011

  1. 1. Building Applications With MongoDB An Introduction With Steve Francia@spf13
  2. 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. 3. Why MongoDB• Open source• Designed for today • Today’s hardware / environments • Today’s challenges• Great developer experience• Reliable• Scalable
  4. 4. Use Cases • RDBMS replacement for high-traffic web applications • Content Management-type applications • Real-time analytics • High-speed data loggingWeb 2.0, Media, SaaS, Gaming, Finance, Telecom, Healthcare
  5. 5. In Good Company
  6. 6. Let’s Design An App• Users Check-in to a Location• Leave Notes and Comments About That Location• Requirements, then Documents
  7. 7. Terminology RDBMS MongoDBTable, View ➜ CollectionRow ➜ JSON DocumentIndex ➜ IndexJoin ➜ Embedded DocumentPartition ➜ ShardPartition Key ➜ Shard Key
  8. 8. Documents{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "spf13", date : "Sat Jul 24 2010 19:47:11", text : "MongoSF", tags : [ "San Francisco", "MongoDB" ]} Note: _id is unique, but can be anythingyou’d like
  9. 9. CollectionsDoc 1 Doc 4 Doc 7Doc 2 Doc 5 Doc 8Doc 3 Doc 6 Doc 9Places Users Check-Ins
  10. 10. Application Goals (1) Q: Current location (3) Record user A: Places near location checkins(2) Add user (4) Stats aboutgenerated checkinscontent Places Check ins
  11. 11. Places v1place1 = { name : "10gen HQ", address : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011"}
  12. 12. Places v2place1 = { name : "10gen HQ", address : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011"}> db.posts.find({zip: "10011"}).limit(10)
  13. 13. Places v2place1 = { name : "10gen HQ", address : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011", tags : [ "business", "awesome" ]}
  14. 14. Places v3place1 = { name : "10gen HQ", address : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011", tags : [ "business", "awesome" ]}> db.posts.findOne({ zip: "10011", tags: "awesome" })
  15. 15. Places v3place1 = { name : "10gen HQ", address : "134 5th Avenue 3rd Floor", city : "New York", zip : "10011", tags : [ "business", "awesome" ], latlong : [40.0,72.0]}
  16. 16. Places v3place1 = { 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"})
  17. 17. Places v3place1 = { 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]}})
  18. 18. Places v4place1 = { 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 onWednesdays from 4-6pm"}, {.....}]}
  19. 19. Inserting PlacesInitial Data Load> db.places.insert(place1)
  20. 20. Querying Your Place Creating Your Index> db.places.ensureIndex({latlong : "2d"})> db.places.ensureIndex({ tags : 1 })> db.places.ensureIndex({ name : 1 })
  21. 21. Querying PlacesFinding Places > db.places.find({latlong:{$near:[40,70]}})With Regular Expressions > db.places.find({name: /^typeAheadString/ })By Tag > db.posts.find({tags: "business" })
  22. 22. Application Goals (1) Q: Current location (3) Record user A: Places near location checkins(2) Add user (4) Stats aboutgenerated checkinscontent Places Check ins
  23. 23. Updating PlacesUsing Update to Add Tips> db.places.update({name:"10gen HQ"},{ $push : {tips: {user:"nosh", time:6/26/2010, tip:"stop by for office hours onWednesdays from 4-6"}}}})
  24. 24. Atomic Updates• $set, $unset, $rename• $push, $pop, $pull, $addToSet• $inc
  25. 25. Application Goals (1) Q: Current location (3) Record user A: Places near location checkins(2) Add user (4) Stats aboutgenerated checkinscontent Places Check ins
  26. 26. Users user1 = { name : "ben", address : "ben@email.com", ... checkins : [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab] }checkins [] = ObjectId reference to check-incollection
  27. 27. Check-Inscheckin1 = { place : "10gen HQ", ts : 9/20/2010 10:12:00, userId : <objectId of user>}
  28. 28. Checking In 2 Operations• Insert check in object [checkin collection]• Update ($push) user object [user collection]
  29. 29. Querying Your PlaceCreating Your Index> db.checkins.ensureIndex({place:1,ts:1})> db.checkins.ensureIndex({ ts:1 })
  30. 30. Application Goals (1) Q: Current location (3) Record user A: Places near location checkins(2) Add user (4) Stats aboutgenerated checkinscontent Places Check ins
  31. 31. Simple Stats> db.checkins.find({place : "10gen HQ"})> db.checkins.find({place : "10genHQ"}).sort({ts:-1}).limit(10)> db.checkins.find({place : "10gen HQ", ts:{ $gt: <midnight>}}).limit()> db.checkins.find().sort({ts:-1}).limit(50)
  32. 32. Stats With MapReducemapFunc = 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: [….., ….., …..]} })
  33. 33. Application Goals (1) Q: Current location (3) Record user(2) Add usergeneratedcontent :)A: Places near location checkins (4) Stats about checkins Places Check ins
  34. 34. MongoDBDeployments
  35. 35. Replica SetsPrimary Primary PrimarySecondary Secondary SecondarySecondary Arbiter Secondary Secondary Secondary
  36. 36. Sharding App App App Server Server Server MongoS MongoS MongoS ConfigD ConfigD ConfigDMongoD MongoD MongoD MongoDMongoD MongoD MongoD MongoDMongoD MongoD MongoD MongoD
  37. 37. Try it for yourself at try.mongodb.org
  38. 38. spf13.com github.com/spf13 @spf13 Questions? download at mongodb.orgWe’re hiring!! Engineers, Sales, Evangelist, Marketing & Support Contact us at jobs@10gen.com

×