Building Your First MongoDB Application

1,404 views
1,336 views

Published on

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.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,404
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
18
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • \n
  • SF - early adopters of MongoDB (0.8), so we went through some of the growing pains\nSQLAlchemy - I don’t hate SQL!\nKnow Python best, but also C, C++, etc.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Tend to not have auto-increment keys\nStandard JSON values + a few new primitives\nEmbedded docs & arrays OK (and encouraged!)\n
  • \n
  • Lots of places, hard to find the one you need\n
  • Better, now at least we’re just looking for hotels, still too many\n
  • Results sorted by distance\n
  • Embedded tips for data locality\n
  • Regex has same benefits and drawbacks as LIKE in sql -- prefix queries are your friend!\n
  • Lots of in-place operators so you can update your document atomically (makes transactionlessness less important)\n
  • So we’ve mostly covered the “Places” side, now let’s look at “Checkins”\n
  • \n
  • Checking in inserts checkin and then $pushes user object\n
  • You can also “reach inside” your objects for updating\n
  • \n
  • \n
  • Flexible output options for dumping results in a collection\n
  • \n
  • \n
  • Data automatically balanced between replica sets\nConfig server stores ‘chunk’ locations\nTransparent to applications\n
  • \n
  • No transactions, but doc model is rich enough to do without in many apps\nNo server-side JOIN, but can be emulated in client (use $in)\nNo referential integrity, but 1:N joins become embedded documents\nData model *can* be relational, but you give up performance (and introduce integrity issues, etc) - many will just denormalize data to get both flexibility and performance at the expense of application complexity.\n
  • \n
  • Building Your First MongoDB Application

    1. 1. Building your FirstMongoDB Application Rick Copeland @rick446 http://arborian.com
    2. 2. Who am I?• Now a consultant, but formerly... • Software engineer at SourceForge • Author of Essential SQLAlchemy • Primarily code Python
    3. 3. What will you learn here?
    4. 4. What will you learn here?• Data Modeling
    5. 5. What will you learn here?• Data Modeling• Queries
    6. 6. What will you learn here?• Data Modeling• Queries• Geospatial indexing
    7. 7. What will you learn here?• Data Modeling• Queries• Geospatial indexing• Updates
    8. 8. What will you learn here?• Data Modeling• Queries• Geospatial indexing• Updates• Map/Reduce
    9. 9. What will you learn here?• Data Modeling• Queries• Geospatial indexing• Updates• Map/Reduce• Deployment and Scaling Concerns
    10. 10. What will you learn here?• Data Modeling• Queries• Geospatial indexing• Updates• Map/Reduce• Deployment and Scaling Concerns• Why MongoDB?
    11. 11. 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)
    12. 12. 3tri Operations Record Find nearby checkins places Places CheckinsUser-generated Checkin content Stats
    13. 13. MongoDB Terminology Relational MongoDB Database Database Table Collection Index Index Row Document Column Field
    14. 14. Documents?doc1 = {! _id: ObjectId(4b97e62bf1d8c7152c9ccb74), key1: value1,! key2: value2, key3: {..., ..., ...}, key4: [..., ..., ]}
    15. 15. Collections • No schema enforcementdoc1,doc2,... doc5,doc6,... • Per-collection... Places Users • Querying • Indexing doc8,doc9,... • Updating Checkins • Sharding
    16. 16. Places v1place1 = {! name: "Blake Hotel",! address: "555 South McDowell Street", city: "Charlotte", zip: "28204"}db.places.find({zip:"28204"}).limit(10)
    17. 17. Places v2place2 = { name: "Blake Hotel",! address: "555 South McDowell Street", city: "Charlotte", zip: "28204", tags: ["hotel", "recommended"]}db.places.find({ zip: "28204", tags: "hotel"}).limit(10)
    18. 18. Places v3place3 = { 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]}})
    19. 19. Places v4place4 = { 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"}, { ... }, { ... } ] }
    20. 20. 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"})
    21. 21. 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" } } } )
    22. 22. 3tri Operations Record Find nearby checkins places Places CheckinsUser-generated Checkin content Stats
    23. 23. Usersuser1 = { name: "rick", email: "rick@arborian.com", ... checkins: [ObjectId(4b97e62bf1d8c7152c9ccb74), ... ] }/* checkins [] = references checkin collection _id field */
    24. 24. Checkinsdb.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" } }
    25. 25. Atomic Updates• $set • $pull• $unset • $pullAll• $rename • $addToSet• $push • $inc• $pushAll • $bit• $pop
    26. 26. 3tri Operations Record Find nearby checkins places Places CheckinsUser-generated Checkin content Stats
    27. 27. 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()
    28. 28. MapReducemapFunc = 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}, ...]
    29. 29. Deployment and Scaling Options
    30. 30. Single Master DeploymentRead / Write Primary Read Secondary Read Secondary
    31. 31. Auto-Sharding Shard 1 (0..10) Shard 2 (10..20) Shard 3 (20..30) Primary Primary Primary Secondary Secondary SecondaryConfig Secondary Secondary Secondary
    32. 32. 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
    33. 33. 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)
    34. 34. Questions? Please rate this talk at http://svy.mk/L3jM7fInterested in training? http://Arborian.com/trainingMongoDB Info & Downloads: http://mongodb.org Rick Copeland @rick446 http://arborian.com

    ×