http://mongodb.org http://10gen.com Building applications with MongoDB – An introduction  Roger Bodamer [email_address] @r...
Today ’s Talk <ul><li>Developing your first Web Application with MongoDB </li></ul><ul><li>What is MongoDB, Platforms and ...
Why MongoDB <ul><li>Intrinsic support for agile development </li></ul><ul><li>Super low latency access to your data </li><...
MongoDB <ul><li>Document Oriented Database </li></ul><ul><ul><li>Data is stored in documents, not tables / relations </li>...
Design <ul><li>Want to build an app where users can check in to a location </li></ul><ul><li>Leave notes or comments about...
Requirements <ul><li>Locations </li></ul><ul><ul><li>Need to store locations (Offices, Restaurants etc) </li></ul></ul><ul...
Requirements <ul><li>Locations </li></ul><ul><ul><li>Need to store locations (Offices, Restaurants etc) </li></ul></ul><ul...
Terminology RDBMS Mongo Table, View Collection Row(s) JSON Document Index Index Join Embedded Document Partition Shard Par...
Collections loc1, loc2, loc3 Locations Users User1, User2
JSON Sample Doc <ul><li>{  _id  : ObjectId(&quot;4c4ba5c0672c685e5e8aabf3&quot;), </li></ul><ul><li>author  : &quot;roger&...
BSON <ul><li>JSON has powerful, but limited set of datatypes </li></ul><ul><ul><li>Mongo extends datypes with Date, Int ty...
Locations v1 location1= { name: &quot;10gen East Coast ”, address: ”134 5 th  Avenue 3 rd  Floor”, city: &quot;New York”, ...
Places v1 location1= { name: &quot;10gen East Coast ”, address: ”134 5 th  Avenue 3 rd  Floor”, city: &quot;New York”, zip...
Places v2 location1 = { name: &quot;10gen East Coast ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New Yor...
Places v2 location1 = { name: &quot;10gen East Coast ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New Yor...
Places v3 location1 = { name: &quot;10gen East Coast ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New Yor...
Places v3 location1 = { name: &quot;10gen East Coast ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New Yor...
Places v3 location1 = { name: &quot;10gen HQ ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New York”, zip:...
Places v4 location1 = { name: &quot;10gen HQ ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New York”, zip:...
Querying your Places Creating your indexes db.locations.ensureIndex({tags:1}) db.locations.ensureIndex({name:1}) db.locati...
Inserting and updating locations Initial data load: db.locations.insert(place1) Using update to Add tips: db.locations.upd...
Requirements <ul><li>Locations </li></ul><ul><ul><li>Need to store locations (Offices, Restaurants etc) </li></ul></ul><ul...
Users user1 = { name:  “nosh” email:  “nosh@10gen.com”, . . . checkins: [{   location:  “10gen HQ”, ts: 9/20/2010 10:12:00...
Simple Stats db.users.find({‘checkins.location’:  “10gen HQ”) db.checkins.find({‘checkins.location’:  “10gen HQ”}) .sort({...
Alternative user1 = { name:  “nosh” email:  “nosh@10gen.com”, . . . checkins: [ 4b97e62bf1d8c7152c9ccb74,  5a20e62bf1d8c73...
User Check in Check-in = 2 ops read location to obtain location id Update ($push) location id to user object Queries: find...
Unsharded Deployment <ul><li>Configure as a replica set for automated failover </li></ul><ul><li>Async replication between...
Sharded Deployment <ul><li>Autosharding distributes data among two or more replica sets </li></ul><ul><li>Mongo Config Ser...
Use Cases <ul><li>RDBMS replacement for high-traffic web applications </li></ul><ul><li>Content Management-type applicatio...
http://mongodb.org http://10gen.com 10Gen is hiring!  @mongodb [email_address] @rogerb
Upcoming SlideShare
Loading in...5
×

Mongo Web Apps: OSCON 2011

11,394

Published on

Building web applications with mongodb, an introduction

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

No Downloads
Views
Total Views
11,394
On Slideshare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
50
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • 1 doc can have multiple of locations and index on it.
  • Memory mapped files, BSON, indexes, multiple data types, binary files, etc @ main datasets: places and checkins use cases: given current loc find places nearby; add notes to locations Record checkins Generate stats about checkins
  • Memory mapped files, BSON, indexes, multiple data types, binary files, etc @ main datasets: places and checkins use cases: given current loc find places nearby; add notes to locations Record checkins Generate stats about checkins
  • Documents go into collections Todays app: users , places, checkins
  • Latlong are actually real lat / long points $near gives you the closest 100
  • Latlong are actually real lat / long points $near gives you the closest 100
  • Latlong are actually real lat / long points $near gives you the closest 100
  • Memory mapped files, BSON, indexes, multiple data types, binary files, etc @ main datasets: places and checkins use cases: given current loc find places nearby; add notes to locations Record checkins Generate stats about checkins
  • Mongo Web Apps: OSCON 2011

    1. 1. http://mongodb.org http://10gen.com Building applications with MongoDB – An introduction Roger Bodamer [email_address] @rogerb
    2. 2. Today ’s Talk <ul><li>Developing your first Web Application with MongoDB </li></ul><ul><li>What is MongoDB, Platforms and availability </li></ul><ul><li>Data Modeling, queries and geospatial queries </li></ul><ul><li>Location bases App </li></ul><ul><li>Example uses MongoDB Javascript shell </li></ul>
    3. 3. Why MongoDB <ul><li>Intrinsic support for agile development </li></ul><ul><li>Super low latency access to your data </li></ul><ul><ul><li>Very little CPU overhead </li></ul></ul><ul><li>No Additional caching layer required </li></ul><ul><li>Built in Replication and Horizontal Scaling support </li></ul>
    4. 4. MongoDB <ul><li>Document Oriented Database </li></ul><ul><ul><li>Data is stored in documents, not tables / relations </li></ul></ul><ul><li>MongoDB is Implemented in C++ for best performance </li></ul><ul><li>Platforms 32/64 bit Windows Linux, Mac OS-X, FreeBSD, Solaris </li></ul><ul><li>Language drivers for: </li></ul><ul><ul><li>Ruby / Ruby-on-Rails </li></ul></ul><ul><ul><li>Java </li></ul></ul><ul><ul><li>C# </li></ul></ul><ul><ul><li>JavaScript </li></ul></ul><ul><ul><li>C / C++ </li></ul></ul><ul><ul><li>Erlang Python, Perl others..... and much more ! .. </li></ul></ul>
    5. 5. Design <ul><li>Want to build an app where users can check in to a location </li></ul><ul><li>Leave notes or comments about that location </li></ul><ul><li>Iterative Approach: </li></ul><ul><ul><li>Decide requirements </li></ul></ul><ul><ul><li>Design documents </li></ul></ul><ul><ul><li>Rinse, repeat :-) </li></ul></ul>
    6. 6. Requirements <ul><li>Locations </li></ul><ul><ul><li>Need to store locations (Offices, Restaurants etc) </li></ul></ul><ul><ul><ul><li>Want to be able to store name, address and tags </li></ul></ul></ul><ul><ul><ul><li>Maybe User Generated Content, i.e. tips / small notes ? </li></ul></ul></ul><ul><ul><li>Want to be able to find other locations nearby </li></ul></ul>
    7. 7. Requirements <ul><li>Locations </li></ul><ul><ul><li>Need to store locations (Offices, Restaurants etc) </li></ul></ul><ul><ul><ul><li>Want to be able to store name, address and tags </li></ul></ul></ul><ul><ul><ul><li>Maybe User Generated Content, i.e. tips / small notes ? </li></ul></ul></ul><ul><ul><li>Want to be able to find other locations nearby </li></ul></ul><ul><li>Checkins </li></ul><ul><ul><li>User should be able to ‘check in’ to a location </li></ul></ul><ul><ul><li>Want to be able to generate statistics </li></ul></ul>
    8. 8. Terminology RDBMS Mongo Table, View Collection Row(s) JSON Document Index Index Join Embedded Document Partition Shard Partition Key Shard Key
    9. 9. Collections loc1, loc2, loc3 Locations Users User1, User2
    10. 10. JSON Sample Doc <ul><li>{ _id : ObjectId(&quot;4c4ba5c0672c685e5e8aabf3&quot;), </li></ul><ul><li>author : &quot;roger&quot;, </li></ul><ul><li>date : &quot;Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)&quot;, </li></ul><ul><li>text : ”MongoSF&quot;, </li></ul><ul><li>tags : [ ”San Francisco&quot;, ”MongoDB&quot; ] } </li></ul><ul><li>Notes: </li></ul><ul><li>- _id is unique, but can be anything you’d like </li></ul>
    11. 11. BSON <ul><li>JSON has powerful, but limited set of datatypes </li></ul><ul><ul><li>Mongo extends datypes with Date, Int types, Id, … </li></ul></ul><ul><li>MongoDB stores data in BSON </li></ul><ul><li>BSON is a binary representation of JSON </li></ul><ul><ul><li>Optimized for performance and navigational abilities </li></ul></ul><ul><ul><li>Also compression </li></ul></ul><ul><ul><li>See bsonspec.org </li></ul></ul>
    12. 12. Locations v1 location1= { name: &quot;10gen East Coast ”, address: ”134 5 th Avenue 3 rd Floor”, city: &quot;New York”, zip: &quot;10011” }
    13. 13. Places v1 location1= { name: &quot;10gen East Coast ”, address: ”134 5 th Avenue 3 rd Floor”, city: &quot;New York”, zip: &quot;10011” } db.locations.find({zip: ”10011”}).limit(10)
    14. 14. Places v2 location1 = { name: &quot;10gen East Coast ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New York”, zip: &quot;10011”, tags: [ “business”, “mongodb”] }
    15. 15. Places v2 location1 = { name: &quot;10gen East Coast ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New York”, zip: &quot;10011”, tags: [ “business”, “mongodb”] } db.locations.find({zip: ”10011”, tags:”business”})
    16. 16. Places v3 location1 = { name: &quot;10gen East Coast ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New York”, zip: &quot;10011”, tags: [ “business”, “mongodb”], latlong: [40.0,72.0] }
    17. 17. Places v3 location1 = { name: &quot;10gen East Coast ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New York”, zip: &quot;10011”, tags: [ “business”, “cool place”], latlong: [40.0,72.0] } db.locations.ensureIndex({latlong: ”2d”})
    18. 18. Places v3 location1 = { name: &quot;10gen HQ ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New York”, zip: &quot;10011”, tags: [ “business”, “cool place”], latlong: [40.0,72.0] } db.locations.ensureIndex({latlong: ”2d”}) db.locations.find({latlong:{$near:[40,70]}})
    19. 19. Places v4 location1 = { name: &quot;10gen HQ ”, address: &quot;17 West 18th Street 8th Floor”, city: &quot;New York”, zip: &quot;10011”, latlong: [40.0,72.0], tags: [ “business”, “cool place”], tips: [ {user:&quot;nosh&quot;, time:6/26/2010, tip:&quot;stop by for office hours on Wednesdays from 4-6pm&quot;}, {.....}, ] }
    20. 20. Querying your Places Creating your indexes db.locations.ensureIndex({tags:1}) db.locations.ensureIndex({name:1}) db.locations.ensureIndex({latlong: ”2d”}) Finding places: db. locations .find({latlong:{$near:[40,70]}}) With regular expressions: db. locations .find({name: /^ typeaheadstring /) By tag: db. locations .find({tags: “business”})
    21. 21. Inserting and updating locations Initial data load: db.locations.insert(place1) Using update to Add tips: db.locations.update({name:&quot;10gen HQ&quot;}, {$push :{tips: {user:&quot;nosh&quot;, time:6/26/2010, tip:&quot;stop by for office hours on Wednesdays from 4-6&quot;}}}}
    22. 22. Requirements <ul><li>Locations </li></ul><ul><ul><li>Need to store locations (Offices, Restaurants etc) </li></ul></ul><ul><ul><ul><li>Want to be able to store name, address and tags </li></ul></ul></ul><ul><ul><ul><li>Maybe User Generated Content, i.e. tips / small notes ? </li></ul></ul></ul><ul><ul><li>Want to be able to find other locations nearby </li></ul></ul><ul><li>Checkins </li></ul><ul><ul><li>User should be able to ‘check in’ to a location </li></ul></ul><ul><ul><li>Want to be able to generate statistics </li></ul></ul>
    23. 23. Users user1 = { name: “nosh” email: “nosh@10gen.com”, . . . checkins: [{ location: “10gen HQ”, ts: 9/20/2010 10:12:00, … }, … ] }
    24. 24. Simple Stats db.users.find({‘checkins.location’: “10gen HQ”) db.checkins.find({‘checkins.location’: “10gen HQ”}) .sort({ts:-1}).limit(10) db.checkins.find({‘checkins.location’: “10gen HQ”, ts: {$gt: midnight}}).count()
    25. 25. Alternative user1 = { name: “nosh” email: “nosh@10gen.com”, . . . checkins: [ 4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab ] } checkins [] = ObjectId reference to locations collection
    26. 26. User Check in Check-in = 2 ops read location to obtain location id Update ($push) location id to user object Queries: find all locations where a user checked in: checkin_array = db.users.find({..}, {checkins:true}).checkins db.location.find({_id:{$in: checkin_array}})
    27. 27. Unsharded Deployment <ul><li>Configure as a replica set for automated failover </li></ul><ul><li>Async replication between nodes </li></ul><ul><li>Add more secondaries to scale reads </li></ul>Secondary Primary Secondary
    28. 28. Sharded Deployment <ul><li>Autosharding distributes data among two or more replica sets </li></ul><ul><li>Mongo Config Server(s) handles distribution & balancing </li></ul><ul><li>Transparent to applications </li></ul>Secondary Primary MongoS config
    29. 29. Use Cases <ul><li>RDBMS replacement for high-traffic web applications </li></ul><ul><li>Content Management-type applications </li></ul><ul><li>Real-time analytics </li></ul><ul><li>High-speed data logging </li></ul>Web 2.0, Media, SaaS, Gaming, Finance, Telecom, Healthcare
    30. 30. http://mongodb.org http://10gen.com 10Gen is hiring! @mongodb [email_address] @rogerb
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×