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

Building Your First MongoDB Application

Avatar for rick446 rick446
June 09, 2012

Building Your First MongoDB Application

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.

Avatar for rick446

rick446

June 09, 2012
Tweet

More Decks by rick446

Other Decks in Technology

Transcript

  1. Who am I? • Now a consultant, but formerly... •

    Software engineer at SourceForge • Author of Essential SQLAlchemy • Primarily code Python Saturday, June 9, 12
  2. What will you learn here? • Data Modeling • Queries

    • Geospatial indexing Saturday, June 9, 12
  3. What will you learn here? • Data Modeling • Queries

    • Geospatial indexing • Updates Saturday, June 9, 12
  4. What will you learn here? • Data Modeling • Queries

    • Geospatial indexing • Updates • Map/Reduce Saturday, June 9, 12
  5. What will you learn here? • Data Modeling • Queries

    • Geospatial indexing • Updates • Map/Reduce • Deployment and Scaling Concerns Saturday, June 9, 12
  6. What will you learn here? • Data Modeling • Queries

    • Geospatial indexing • Updates • Map/Reduce • Deployment and Scaling Concerns • Why MongoDB? Saturday, June 9, 12
  7. Our Application • Users can create different locations • Users

    can “check in” to these locations • Users can see who else is checked in • Let’s call it ThreeTriangles (3tri) Saturday, June 9, 12
  8. Documents? doc1 = {! _id: ObjectId('4b97e62bf1d8c7152c9ccb74'), key1: value1,! key2: value2,

    key3: {..., ..., ...}, key4: [..., ..., ] } Saturday, June 9, 12
  9. Collections • No schema enforcement • Per-collection... • Querying •

    Indexing • Updating • Sharding doc1,doc2,... doc5,doc6,... doc8,doc9,... Places Users Checkins Saturday, June 9, 12
  10. Places v1 place1 = {! name: "Blake Hotel",! address: "555

    South McDowell Street", city: "Charlotte", zip: "28204"} db.places.find({zip:"28204"}).limit(10) Saturday, June 9, 12
  11. Places v2 place2 = { name: "Blake Hotel",! address: "555

    South McDowell Street", city: "Charlotte", zip: "28204", tags: ["hotel", "recommended"]} db.places.find({ zip: "28204", tags: "hotel"}).limit(10) Saturday, June 9, 12
  12. Places v3 place3 = { name: "Blake Hotel",! address: "555

    South McDowell Street", city: "Charlotte", zip: "28204", tags: ["hotel", "recommended"], latlon: [ 35.21, 80.83 ] } db.places.ensureIndex({latlong:"2d"}) db.places.find({latlong:{$near:[35,80]}}) Saturday, June 9, 12
  13. Places v4 place4 = { name: "Blake Hotel",! address: "555

    South McDowell Street", city: "Charlotte", zip: "28204", tags: ["hotel", "recommended"], latlon: [ 35.21, 80.83 ], tips: [ { user: "rick", time: ISODateTime(...), tip: "Come learn about #self2012"}, { ... }, { ... } ] } Saturday, June 9, 12
  14. Some Queries /* First, some indexes */ db.places.ensureIndex({tags:1}) db.places.ensureIndex({name:1}) db.places.ensureIndex({latlong:"2d"})

    /* Find places */ db.places.find({latlong:{$near:[40,70]}}) /* Regex searching */ db.places.find({name: /^typeaheadstring/) /* Searching arrays */ db.places.find({tags: "business"}) Saturday, June 9, 12
  15. Inserting and updating /* Initial data load */ db.places.insert([place1, place2,

    ...]) /* Update-in-place */ db.places.update( { name:"Blake Hotel" }, { $push : { tips: { user: "rick", time: ISODateTime(...), tip: "Come learn about #self2012" } } } ) Saturday, June 9, 12
  16. Users user1 = { name: "rick", email: "rick@arborian.com", ... checkins:

    [ ObjectId('4b97e62bf1d8c7152c9ccb74'), ... ] } /* checkins [] = references checkin collection _id field */ Saturday, June 9, 12
  17. Checkins db.checkins.ensureIndex({place:1, ts:1}) db.checkins.ensureIndex({ts:1}) checkin1 = { place: { id:

    ObjectId(...), name: "Blake Hotel" }, ts: ISODateTime(...), user: { id: ObjectId(...), name: "rick" } } Saturday, June 9, 12
  18. Atomic Updates • $set • $unset • $rename • $push

    • $pushAll • $pop • $pull • $pullAll • $addToSet • $inc • $bit Saturday, June 9, 12
  19. Simple Statistics /* All checkins */ db.checkins.find({"place.name": "Blake Hotel"}) /*

    Last 10 checkins */ db.checkins.find({"place.name": "Blake Hotel"}) .sort({ts:-1}).limit(10) /* Number of checkins today */ db.checkins.find( { "place.name": "Blake Hotel", ts: { $gt: ISODateTime(...)} }) .count() Saturday, June 9, 12
  20. MapReduce mapFunc = function() { emit(this.place.name, 1); } reduceFunc =

    function(key, values) { return Array.sum(values); } res = db.checkins.mapReduce( mapFunc, reduceFunc, ! { query: { ts: { $gt: nowminus3hrs } } out: {inline: 1} }) res.results = [ {_id:"Blake Hotel", value: 17}, ...] Saturday, June 9, 12
  21. Auto-Sharding Shard 1 (0..10) Primary Secondary Secondary Shard 2 (10..20)

    Primary Secondary Secondary Shard 3 (20..30) Primary Secondary Secondary Config Saturday, June 9, 12
  22. Use Cases • Replace RDBMS for high-traffic web applications •

    CMS-style applications • Social and mobile applications • Real-time analytics, high-speed logging • Maybe not double-entry bookkeeping Saturday, June 9, 12
  23. What do you give up? • No multi-document atomic operations

    (i.e. transactions) • No server-side JOINs • No referential integrity constraints between documents • Data model is typically tied to query patterns (less flexible than relational DBs) Saturday, June 9, 12
  24. Questions? Please rate this talk at http://svy.mk/L3jM7f Interested in training?

    http://Arborian.com/training MongoDB Info & Downloads: http://mongodb.org Rick Copeland @rick446 http://arborian.com Saturday, June 9, 12