Building Applications with MongoDB - an Introduction
description
Transcript of Building Applications with MongoDB - an Introduction
![Page 1: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/1.jpg)
Nosh [email protected]@noshinoshhttp://mongodb.orghttp://10gen.com
Building applications with MongoDB – An introduction
MongoBerlin – March 25, 2011
![Page 2: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/2.jpg)
Today’s Talk•MongoDB: Data modeling, queries,
geospatial, updates, map reduce
•Using a location-based app as an example
•Example Works in MongoDB JS shell
![Page 3: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/3.jpg)
Application Goals
PlacesCheck
ins
(1)Q: Current locationA: Places near location
(2) Add user generated content
(3) Record user checkins
(4) Stats about checkins
![Page 4: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/4.jpg)
Documents
doc1 = {_id: 4b97e62bf1d8c7152c9ccb74,key1: value1,key2: value2,
key3: {..., ..., ...},
key4: [..., ..., ]
}
![Page 5: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/5.jpg)
Collections
doc1, doc2, doc3
Places Users Checkins
doc3, doc4, doc5
doc6, doc7, doc8
![Page 6: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/6.jpg)
place1 = {name: "10gen HQ”,address: ”134 5th Avenue 3rd Floor”,city: "New York”,zip: "10011”
}
db.places.find({zip:”10011”}).limit(10)
Places v1
![Page 7: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/7.jpg)
place1 = {name: "10gen HQ”,address: "17 West 18th Street 8th Floor”,city: "New York”,zip: "10011”,
tags: [“business”, “recommended”]
}
db.places.find({zip:”10011”, tags:”business”})
Places v2
![Page 8: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/8.jpg)
place1 = {name: "10gen HQ”,address: "17 West 18th Street 8th Floor”,city: "New York”,zip: "10011”,
tags: [“business”, “cool place”],
latlong: [40.0,72.0]
}
db.places.ensureIndex({latlong:”2d”})db.places.find({latlong:{$near:[40,70]}})
Places v3
![Page 9: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/9.jpg)
place1 = {name: "10gen HQ”,address: "17 West 18th Street 8th 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 for office
hours on Wednesdays from 4-6pm"}, {.....}, {.....}]
}
Places v4
![Page 10: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/10.jpg)
Creating your indexesdb.places.ensureIndex({tags:1})db.places.ensureIndex({name:1})db.places.ensureIndex({latlong:”2d”})
Finding places:db.places.find({latlong:{$near:[40,70]}})
With regular expressions:db.places.find({name: /^typeaheadstring/)
By tag:db.places.find({tags: “business”})
Querying your Places
![Page 11: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/11.jpg)
Initial data load:db.places.insert(place1)
Updating tips:db.places.update({name:"10gen HQ"},
{$push :{tips: {user:"nosh", time:6/26/2010,
tip:"stop by for office hours on Wednesdays from 4-6"}}}}
Inserting and updating places
![Page 12: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/12.jpg)
Application Goals
PlacesCheck
ins
(1)Q: Current locationA: Places near location
(2) Add user generated content
(3) Record user checkins
(4) Stats about checkins
![Page 13: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/13.jpg)
user1 = {name: “nosh”email: “[email protected]”,...checkins: [4b97e62bf1d8c7152c9ccb74,
5a20e62bf1d8c736ab]}
checkins [] = ObjectId reference to checkin collection
Users
![Page 14: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/14.jpg)
checkin1 = {place: “10gen HQ”,
ts: 9/20/2010 10:12:00,
userId: <objectid of user>}
Check-in = 2 opsInsert check in object [checkin collection]Update ($push) user object [user collection]
Indexes:db.checkins.ensureIndex({place:1, ts:1})db.checkins.ensureIndex({ts:1})
Checkins
![Page 15: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/15.jpg)
$set, $unset, $rename
$push, $pop, $pull, $addToSet
$inc
Atomic Updates
![Page 16: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/16.jpg)
Application Goals
PlacesCheck
ins
(1)Q: Current locationA: Places near location
(2) Add user generated content
(3) Record user checkins
(4) Stats about checkins
![Page 17: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/17.jpg)
Simple Statsdb.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()
![Page 18: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/18.jpg)
Stats with 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}, ….., ….]
res.find({ value: {$gt: 15},_id: {$in: [….., ….., …..]}
})
![Page 19: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/19.jpg)
Application Goals
PlacesCheck
ins
(1)Q: Current locationA: Places near location
(2) Add user generated content
(3) Record user checkins
(4) Stats about checkins
![Page 20: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/20.jpg)
Single Master Deployments
Primary/Master
Secondary/Slave• Configure as a replica set for automated
failover
• Add more secondaries to scale reads
![Page 21: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/21.jpg)
Auto Sharded Deployment
Primary/Master
Secondary/Slave
MongoS
•Autosharding distributes data among two or more replica sets• Mongo Config Server(s) handles distribution & balancing
• Transparent to applications
MongoConfig
![Page 22: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/22.jpg)
Use Cases•RDBMS replacement for high-traffic web
applications
•Content Management-type applications
•Social, mobile
•Real-time analytics
•High-speed data loggingWeb 2.0, Media, SaaS, Gaming, Finance, Telecom, Healthcare
![Page 23: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/23.jpg)
MongoDB in Production
![Page 24: Building Applications with MongoDB - an Introduction](https://reader033.fdocuments.in/reader033/viewer/2022061118/5469e53daf7959653c8b628c/html5/thumbnails/24.jpg)
Nosh [email protected] of Product Strategy, 10genhttp://mongodb.orghttp://10gen.com
-We are hiring!-@mongodb
[email protected]@noshinosh