Building applications with Mongo DB An introduction Roger
Building applications with Mongo. DB – An introduction Roger Bodamer roger@10 gen. com @rogerb http: //mongodb. org http: //10 gen. com
Today’s Talk • Developing your first Web Application with Mongo. DB • What is Mongo. DB, Platforms and availability • Data Modeling, queries and geospatial queries • Location bases App • Example uses Mongo. DB Javascript shell
Why Mongo. DB • Intrinsic support for agile development • Super low latency access to your data – Very little CPU overhead • No Additional caching layer required • Built in Replication and Horizontal Scaling support
Mongo. DB • Document Oriented Database – Data is stored in documents, not tables / relations • Mongo. DB is Implemented in C++ for best performance • Platforms 32/64 bit Windows Linux, Mac OS-X, Free. BSD, Solaris • Language drivers for: – Ruby / Ruby-on-Rails – Java – C# – Java. Script – C / C++ – Erlang Python, Perl others. . . and much more !. .
Design • Want to build an app where users can check in to a location • Leave notes or comments about that location • Iterative Approach: – Decide requirements – Design documents – Rinse, repeat : -)
Requirements • Locations – Need to store locations (Offices, Restaurants etc) • Want to be able to store name, address and tags • Maybe User Generated Content, i. e. tips / small notes ? – Want to be able to find other locations nearby
Requirements • Locations – Need to store locations (Offices, Restaurants etc) • Want to be able to store name, address and tags • Maybe User Generated Content, i. e. tips / small notes ? – Want to be able to find other locations nearby • Checkins – User should be able to ‘check in’ to a location – Want to be able to generate statistics
Terminology RDBMS Table, View Row(s) Index Join Partition Key Mongo Collection JSON Document Index Embedded Document Shard Key
Collections loc 1, loc 2, loc 3 Locations User 1, User 2 Users
JSON Sample Doc { _id : Object. Id("4 c 4 ba 5 c 0672 c 685 e 5 e 8 aabf 3"), author : "roger", date : "Sat Jul 24 2010 19: 47: 11 GMT-0700 (PDT)", text : ”Mongo. SF", tags : [ ”San Francisco", ”Mongo. DB" ] } Notes: - _id is unique, but can be anything you’d like
BSON • JSON has powerful, but limited set of datatypes – Mongo extends datypes with Date, Int types, Id, … • Mongo. DB stores data in BSON • BSON is a binary representation of JSON – Optimized for performance and navigational abilities – Also compression – See bsonspec. org
Locations v 1 location 1= { name: "10 gen East Coast”, address: ” 134 5 th Avenue 3 rd Floor”, city: "New York”, zip: "10011” }
Places v 1 location 1= { name: "10 gen East Coast”, address: ” 134 5 th Avenue 3 rd Floor”, city: "New York”, zip: "10011” } db. locations. find({zip: ” 10011”}). limit(10)
Places v 2 location 1 = { name: "10 gen East Coast”, address: "17 West 18 th Street 8 th Floor”, city: "New York”, zip: "10011”, tags: [“business”, “mongodb”] }
Places v 2 location 1 = { name: "10 gen East Coast”, address: "17 West 18 th Street 8 th Floor”, city: "New York”, zip: "10011”, tags: [“business”, “mongodb”] } db. locations. find({zip: ” 10011”, tags: ”business”})
Places v 3 location 1 = { name: "10 gen East Coast”, address: "17 West 18 th Street 8 th Floor”, city: "New York”, zip: "10011”, tags: [“business”, “mongodb”], latlong: [40. 0, 72. 0] }
Places v 3 location 1 = { name: "10 gen East Coast”, address: "17 West 18 th Street 8 th Floor”, city: "New York”, zip: "10011”, tags: [“business”, “cool place”], latlong: [40. 0, 72. 0] } db. locations. ensure. Index({latlong: ” 2 d”})
location 1 = { Places v 3 name: "10 gen HQ”, address: "17 West 18 th Street 8 th Floor”, city: "New York”, zip: "10011”, tags: [“business”, “cool place”], latlong: [40. 0, 72. 0] } db. locations. ensure. Index({latlong: ” 2 d”}) db. locations. find({latlong: {$near: [40, 70]}})
location 1 = { Places v 4 name: "10 gen HQ”, address: "17 West 18 th Street 8 th 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 hours on Wednesdays from 4 -6 pm"}, {. . . }, ] } for office
Querying your Places Creating your indexes db. locations. ensure. Index({tags: 1}) db. locations. ensure. Index({name: 1}) db. locations. ensure. Index({latlong: ” 2 d”}) Finding places: db. locations. find({latlong: {$near: [40, 70]}}) With regular expressions: db. locations. find({name: /^typeaheadstring/) By tag: db. locations. find({tags: “business”})
Inserting and updating locations Initial data load: db. locations. insert(place 1) Using update to Add tips: db. locations. update({name: "10 gen HQ"}, {$push : {tips: {user: "nosh", time: 6/26/2010, tip: "stop by for office hours on Wednesdays from 4 -6"}}}}
Requirements • Locations – Need to store locations (Offices, Restaurants etc) • Want to be able to store name, address and tags • Maybe User Generated Content, i. e. tips / small notes ? – Want to be able to find other locations nearby • Checkins – User should be able to ‘check in’ to a location – Want to be able to generate statistics
Users user 1 = { name: “nosh” email: “nosh@10 gen. com”, . . . checkins: [{ location: “ 10 gen HQ”, ts: 9/20/2010 10: 12: 00, …}, … ] }
Simple Stats db. users. find({‘checkins. location’: “ 10 gen HQ”) db. checkins. find({‘checkins. location’: “ 10 gen HQ”}). sort({ts: -1}). limit(10) db. checkins. find({‘checkins. location’: “ 10 gen HQ”, ts: {$gt: midnight}}). count()
Alternative user 1 = { name: “nosh” email: “nosh@10 gen. com”, . . . checkins: [4 b 97 e 62 bf 1 d 8 c 7152 c 9 ccb 74, 5 a 20 e 62 bf 1 d 8 c 736 ab] } checkins [] = Object. Id reference to locations collection
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}})
Unsharded Deployment Primary • Configure as a replica set for automated failover • Async replication between nodes • Add more secondaries to scale reads Secondary
Sharded Deployment Mongo. S Primary confi g Secondary • Autosharding distributes data among two or more replica sets • Mongo Config Server(s) handles distribution & balancing • Transparent to applications
Use Cases • RDBMS replacement for high-traffic web applications • Content Management-type applications • Real-time analytics • High-speed data logging Web 2. 0, Media, Saa. S, Gaming, Finance, Telecom, Healthcare
10 Gen is hiring! @mongodb roger@10 gen. com @rogerb http: //mongodb. org http: //10 gen. com
- Slides: 30