Share
Embed
Building Your First MongoDB Application Webinar
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.
mongodb
March 15, 2012
Transcript
24 slides
-
Building Your First Application Please stand by... We’ll begin shortly.
1-877-668-4493 or +1-408-600-3600 Access code: 663 732 878 Thursday, March 15, 12 -
Step 1 - What is mongoDB?? • Scalable, High-Performance, Open
Source, Document- Oriented Database – JSON (well... BSON) Storage Model – Indexes and Full Query Language – Easy for Developers to Pick Up • More Features at http://www.mongodb.org/ • Overview Video: http://www.10gen.com/what-is-mongodb 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Step 2 - Stop Thinking Relational 1-877-668-4493 or +1-408-600-3600 ~
Access code: 663 732 878 Thursday, March 15, 12 -
Tables to Documents { title: ‘MongoDB’, contributors: [ { name:
‘Eliot Horowitz’, email: ‘[email protected]’ }, { name: ‘Dwight Merriman’, email: ‘[email protected]’ } ], model: { relational: false, awesome: true } } 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Parallels RDBMS MongoDB Table Collection Row Document Column Field Index
Index Join Embedding / Linking Linking Schema Object 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Rich Data Model { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger",
date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : "Spirited Away", tags : [ "Tezuka", "Manga" ], comments : [ { author : "Fred", date : "Sat Jul 24 2010 20:51:03 GMT-0700 (PDT)", text : "Best Movie Ever" } ]} 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Querying >db.posts.find() { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date
: "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : "Spirited Away", tags : [ "Tezuka", "Manga" ] } Notes: - _id is unique, but can be anything you’d like 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Secondary Indexes Create index on any Field in Document //
1 means ascending, -1 means descending >db.posts.ensureIndex({author: 1}) >db.posts.find({author: 'roger'}) { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", ... } 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Secondary Indexes // Index nested documents > db.posts.ensureIndex( “comments.author”:1 )
Ødb.posts.find({‘comments.author’:’Fred’}) // Index on tags > db.posts.ensureIndex( tags: 1) > db.posts.find( { tags: ’Manga’ } ) // geospatial index > db.posts.ensureIndex( “author.location”: “2d” ) > db.posts.find( “author.location” : { $near : [22,42] } ) 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Atomic Operations • $set, $unset, $inc, $push, $pushAll, $pull, $pullAll,
$bit > comment = { author: “fred”, date: new Date(), text: “Best Movie Ever”} > db.posts.update( { _id: “...” }, $push: {comments: comment} ); 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Step 3 - Let’s Build A Sample App Social Media
Check-Ins Q: Current location A: Places near location User Generated Content Places Thursday, March 15, 12 -
Inserting a Place var p = { name: “10gen HQ”,
address: “578 Broadway, 7th Floor”, city: “New York”, zip: “10012”} > db.posts.save(p) 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Let’s Add Some Tags { name: “10gen HQ”, address: “578
Broadway, 7th Floor”, city: “New York”, zip: “10012” tags: [“mongoDB”, “business”]} 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Geo Tags { name: “10gen HQ”, address: “578 Broadway, 7th
Floor”, city: “New York”, zip: “10012” tags: [“mongoDB”, “business”] latlong: [40.0, 72.0]} 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
User-Generated Tips { name: “10gen HQ”, address: “578 Broadway, 7th
Floor”, city: “New York”, zip: “10012” tags: [“mongoDB”, “business”] latlong: [40.0, 72.0] tips: [{user: “kevin”, time: “3/15/2012”,tip: “Make sure to stop by for office hours!”}]} 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Updating Tips db.places.update({name:"10gen HQ"}, {$push :{tips: {user:"nosh", time:3/15/2012, tip:"stop by
for office hours on Wednesdays from 4-6"}}}} 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Querying Our Places • Creating Indexes ★ db.places.ensureIndex({tags:1}) ★ db.places.ensureIndex({name:1})
★ db.places.ensureIndex({latlong:”2d”}) • Finding Places ★ db.places.find({latlong:{$near:[40,70]}}) • Regular Expressions ★ db.places.find({name: /^typeaheadstring/) • Using Tags ★ db.places.find({tags: “business”}) Thursday, March 15, 12 -
User Check-Ins Record User Check-Ins Check-Ins Users Stats Users Stats
Thursday, March 15, 12 -
Users user1 = { name: “Kevin Hanson” e-mail: “[email protected]”, check-ins:
[4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab] } checkins [] = ObjectId reference to Check-Ins Collection 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Check-Ins user1 = { place: “10gen HQ”, ts: 9/20/2010 10:12:00,
userId: <object id of user> } Every Check-In is Two Operations • Insert a Check-In Object (check-ins collection) • Update ($push) user object with check-in ID (users collection) Thursday, March 15, 12 -
Simple Stats db.checkins.find({place: “10gen HQ”) db.checkins.find({place: “10gen HQ”}) .sort({ts:-1}).limit(10) db.checkins.find({place:
“10gen HQ”, ts: {$gt: midnight}}).count() Thursday, March 15, 12 -
Stats w/ MapReduce mapFunc = function() {emit(this.place, 1);} reduceFunc =
function(key, values) {return Array.sum(values);} res = db.checkins.mapReduce(mapFunc,reduceFunc, {query: {timestamp: {$gt:nowminus3hrs}}}) res = [{_id:”10gen HQ”, value: 17}, ….., ….] ... or try using the new aggregation framework! 1-877-668-4493 or +1-408-600-3600 ~ Access code: 663 732 878 Thursday, March 15, 12 -
Final Step - Scale Your App Primary Secondary Secondary Asynchronous
Replication Read Write Read Read Add Secondaries for High Availability / Read Scale High Write Load / Big Data Set -> Use mongoDB Range Based Auto Sharding (A Future Webinar / mongoDB Day) Thursday, March 15, 12 -
@mongodb conferences, appearances, and meetups http://www.10gen.com/events http://bit.ly/mongo> Facebook
| Twitter | LinkedIn http://linkd.in/joinmongo More info at http://www.mongodb.org/ Thursday, March 15, 12