Slide 1

Slide 1 text

An Introduction With Randall Hunt Building Applications With MongoDB @jrhunt

Slide 2

Slide 2 text

Agenda MongoDB: Data modeling, queries, geospatial, updates, map reduce Using a location-based app as an example Example Works in MongoDB JS shell • • •

Slide 3

Slide 3 text

Why MongoDB Open source Designed for today Today’s hardware / environments Today’s challenges Great developer experience Reliable • • • • • •

Slide 4

Slide 4 text

Use Cases Web, Media, SaaS, Gaming, Finance, Telecom, Healthcare, LHC RDBMS replacement for high-traffic web applications Content Management-type applications Real-time analytics High-speed data logging • • • •

Slide 5

Slide 5 text

In Good Company

Slide 6

Slide 6 text

Let’s Design An App Users Check-in to a Location Leave Notes and Comments About That Location Requirements, then Documents • • •

Slide 7

Slide 7 text

Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ JSON Document Index ➜ Index Join ➜ Embedded Document Partition ➜ Shard Partition Key ➜ Shard Key

Slide 8

Slide 8 text

Documents Note: _id is unique, but can be anything you’d like { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "ranman", date : "Sat Jul 24 2010 19:47:11", text : "MongoSF", tags : [ "San Francisco", "MongoDB" ] }

Slide 9

Slide 9 text

Collections Places Users Check-Ins Doc 4 Doc 5 Doc 6 Doc 7 Doc 8 Doc 9 Doc 1 Doc 2 Doc 3

Slide 10

Slide 10 text

Application Goals Places Check ins (1) Q: Current location A: Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins

Slide 11

Slide 11 text

Creating A Document place1 = { name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012" }

Slide 12

Slide 12 text

Inserting Places Initial Data Load > db.places.insert(place1)

Slide 13

Slide 13 text

Query A Collection > db.places.find({zip: "10012"}).limit(10) { _id : ObjectId("01aed46179d968fc55c7232") name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012" }

Slide 14

Slide 14 text

Places v2 place1 = { name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ] }

Slide 15

Slide 15 text

Update A Collection > db.places.update({zip: "10012"}, { tags : [ "business", "awesome" ] }); > db.places.find({_id: ObjectId(...)}) { _id : ObjectId(...) tags : [ "business", "awesome" ] }

Slide 16

Slide 16 text

Updating A Collection > db.places.update({zip: "10012"}, { $set : { tags : [ "business", "awesome" ] } } )

Slide 17

Slide 17 text

Places v2 place1 = { name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ] } > db.posts.findOne({ zip: "10012", tags: "awesome" })

Slide 18

Slide 18 text

$push > db.posts.update( { _id: ObjectId(...)}, { $push: {tags: "moreawesome"}} )

Slide 19

Slide 19 text

.explain() > db.places.find({zip: "10012"}).explain() { "cursor" : "BasicCursor", "nscanned" : 2, "nscannedObjects" : 2, "n" : 1, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : false, "indexOnly" : false, "indexBounds" : { } }

Slide 20

Slide 20 text

create index > db.places.ensureIndex({name: 1}) > db.places.find({name: 1}).explain() ... "cursor" : "BtreeCursor name_1", ... > db.places.ensureIndex({name: 1, zip: 1}) ... "cursor" : "BtreeCursor name_1_zip_1", ....

Slide 21

Slide 21 text

GEO Features place1 = { name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ], latlong : [40.0,72.0] }

Slide 22

Slide 22 text

Adding A GEO Index place1 = { name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ], latlong : [40.0,72.0] } > db.places.ensureIndex({latlong : "2d"})

Slide 23

Slide 23 text

Places v3 place1 = { name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ], latlong : [40.0,72.0] } > db.places.ensureIndex({latlong : "2d"}) > db.places.find({latlong:{$near:[40,70]}})

Slide 24

Slide 24 text

Types of Queries Finding Places With Regular Expressions By Key:Value > db.places.find({latlong:{$near:[40,70]}}) > db.places.find({name: /^typeAheadString/ }) > db.posts.find({tags: "business" })

Slide 25

Slide 25 text

Embedded Documents place1 = { name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10012", tags : [ "business", "awesome" ], latlong : [40.0,72.0], tips : [ { user : "ryan", time : 6/26/2010, tip : "stop by for office hours on Wednesdays from 4-6pm"}, {.....}] }

Slide 26

Slide 26 text

Application Goals Places Check ins (1) Q: Current location A: Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins

Slide 27

Slide 27 text

Updating Places Using Update to Add Tips > db.places.update({name:"10gen HQ"}, { $push : {tips: {user:"ranman", time:6/26/2010, tip:"stop by for office hours on Wednesdays from 4-6"}}}})

Slide 28

Slide 28 text

Atomic Updates $set, $unset, $rename $push, $pop, $pull, $addToSet $inc • • •

Slide 29

Slide 29 text

Application Goals Places Check ins (1) Q: Current location A: Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins

Slide 30

Slide 30 text

Users checkins [] = ObjectId reference to check-in collection user1 = { name : "Randall", address : "randall@10gen.com", ... checkins : [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab] }

Slide 31

Slide 31 text

Check-Ins checkin1 = { place : "10gen HQ", ts : 9/20/2010 10:12:00, userId : }

Slide 32

Slide 32 text

Checking In Insert check in object [checkin collection] Update ($push) user object [user collection] • • 2 Operations

Slide 33

Slide 33 text

Querying Your Place Creating Your Index > db.checkins.ensureIndex({place:1,ts:1}) > db.checkins.ensureIndex({ ts:1 })

Slide 34

Slide 34 text

Application Goals Places Check ins (1) Q: Current location A: Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins

Slide 35

Slide 35 text

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: }}).limit() > db.checkins.find().sort({ts:-1}).limit(50)

Slide 36

Slide 36 text

Stats With MapReduce mapFunc = function() { emit(this.place, 1); } reduceFunc = function(key, values) { return Array.sum(values); } db.checkins.mapReduce(mapFunc,reduceFunc, { query: { timestamp: {$gt:}}, out: result }) result = [{_id:”10gen HQ”, value: 17}, {…..}, {….}] db.result.find({ value: {$gt: 15}, _id: {$in: [….., ….., …..]} })

Slide 37

Slide 37 text

Application Goals Places Check ins (1) Q: Current location A: Places near location (2) Add user generated content (3) Record user checkins (4) Stats about checkins : )

Slide 38

Slide 38 text

MongoDB Deployments

Slide 39

Slide 39 text

Replica Sets Primary Secondary Secondary Primary Secondary Arbiter Primary Secondary Secondary Secondary Secondary

Slide 40

Slide 40 text

App Server App Server App Server Sharding MongoD MongoD MongoD MongoD MongoD MongoD MongoD MongoD MongoD MongoD MongoD MongoD ConfigD ConfigD ConfigD MongoS MongoS MongoS

Slide 41

Slide 41 text

Try it for yourself at try.mongodb.org

Slide 42

Slide 42 text

ranman.com github.com/ranman @jrhunt We’re hiring!! Engineers, Sales, Evangelist, Marketing & Support Contact us at jobs@10gen.com Questions? download at mongodb.org