learn learn how to build your mobile back-end with mongodb
DESCRIPTION
Will Shulman, from MongoLab, shows us how to to persist our mobile app data in the cloud using a super-scalable and amazingly developer-friendly MongoDB back-end.TRANSCRIPT
Mobile apps do not existin a vacuum
• user profile data• logging• location-based data collection• game data (stats, high scores)• polls and surveys• user feedback
Do I want to host thismyself ?
Java + Spring + Hibernate ?Ruby on Rails ?
Hmmm… I will probablyneed to build a REST API…
GET, POST, PUT, DELETE
•EC2•Rackspace•App Engine*
•MySQL•SQLServer•Postgres
•RoR•J2EE
General approach
REST API
• “Scalable, open-source, high-performance, document-orienteddatabase” - 10gen
• “The database your database couldsmell like” - old spice guy
MongoDB is designed to addresstwo modern DB problems
1. object / relational “impedance mismatch”
2. horizontal scalability
A paradigm shift…
• Traditional RDMS– database– table– row
• MongoDB– database– collection– document / object
Documents (a.k.a. Objects){ _id: 1234, author: { name: “Bob Jones”, email: “[email protected]” }, post: “In these troubled times I like to …“, date: { $date: “2010-07-12 13:23UTC” }, location: [ -121.2322, 42.1223222 ], rating: 2.2, comments: [ { user: “[email protected]”, upVotes: 22, downVotes: 14,
text: “Great point! I agree” }, { user: “[email protected]”, upVotes: 421, downVotes: 22,
text: “You are a moron” } ]}
{ _id: 1234, author: { name: “Bob Jones”, email: “[email protected]” }, post: “In these troubled times I like to …“, date: { $date: “2010-07-12 13:23UTC” }, location: [ -121.2322, 42.1223222 ], rating: 2.2, comments: [ { user: “[email protected]”, upVotes: 22, downVotes: 14,
text: “Great point! I agree” }, { user: “[email protected]”, upVotes: 421, downVotes: 22,
text: “You are a moron” } ], tags: [ “politics”, “Virginia” ]}
Flexible “schemas”
db.posts.find({ author.name: “mike” })
Dynamic queries
db.posts.find({ rating: { $gt: 2 }})
db.posts.find({ tags: “software” })
db.posts.find().sort({date: -1}).limit(10)
Comment c = {author: “will”, date: new Date(), text: “great post!”}
db.posts.update({_id: post._id}, {$push: {comments: c}})
Atomic update operators
High performance, fault tolerantclusters made easy
• master / slave replication
• replica sets
• auto-sharding
MongoDB is great for mobile
• everything is JSON!– storage model is JSON– queries are in JSON– update operators are in JSON
• geospatial indexing built-in
• GridFS
Geospatial indexing
• index on geo coordinate pairs
• search by– bounding box– bounding circle (point, radius)
Geospatial indexing
db.places.ensureIndex( { loc: “2d” } )
db.places.find({ loc: { $near : [50, 50] } }).limit(10)
db.places.find({loc: {$within : {$box : [[40,40],[60,60]]}}})
db.places.find({loc: {$within : {$center : [[40,40],10]}}})
Geospatial indexing atFoursquare
• ~1.3M registered users• ~615k check-ins a day• “Who’s here” service tracks last 3 hours for
every user; uses mongo exclusively• all checkins, tips and venues written to
mongo; reads a mix of mongo / legacypostgres
GridFS
• distributed filesystem inside MongoDB
• stores large files by splitting them intosmaller documents
• leverages existing sharding andreplication configuration
• stores metadata along with files
• works well behind a CDN
Street cred
• Shutterfly• Foursquare• bit.ly• Sourceforge• Etsy• The New York Times• Business Insider• Github• Gilt Groupe
• Sugar CRM• Electronic Arts• Evite• CollegeHumor• Disqus• Justin.tv• Chartbeat• Hot potato• Eventbrite
• MongoLab provides cloud-hostedMongoDB
• It’s EC2 hosting + MongoDB + RESTAPI + awesome admin tools
• http://mongolab.com
GET, POST, PUT, DELETE
EC2
MongoDB
Two ways to use MongoLab
REST API
method 1: REST API
method 2: MongoDB driver
Your application
/databases/<d>/collections GET
/databases/<d>/collections/<c> GET POST
/databases/<d>/collections/<c>/<_id> GET PUT DELETE
/databases/<d>/collections/<c>?[q=<query>] [&f=<fields>] [&s=<order>] [&sk=<skip>] [&l=<limit>] GET
MongoLab API
Example app:GeoPost
• POSTURL: https://mongolab.com/api/1/databases/demo/collections/geoposts
data: { “email” : “[email protected]”, “date” : { “$date” : “2010-02-03 10:21:21 UTC” },
“location” : { “lat” : 41, “long” : -82 }, “message” : “Hello Cleveland!” }
• GETURL: https://…/geoposts?q={"location":{"$near":{"lat":38,"long":-122}}}
Android SDK makes client-sideeasy
• JSON: org.json.*• REST: org.apache.http.*
HttpClient client = new DefaultHttpClient()HttpGet request = new HttpGet(url);HttpResponse response = client.execute(request);HttpEntity entity = response.getEntity();InputStream in = entity.getContent();String json = convertStreamToString(in);JSONObject o = new JSONObject(json);
For more info…
• MongoDB / 10Gen– http://mongodb.org– http://10gen.com
• MongoLab– http://mongolab.com– [email protected]