Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An Evening with MongoDB Toronto 2012 - Building Applications with MongoDB

mongodb
August 02, 2012
420

An Evening with MongoDB Toronto 2012 - Building Applications with MongoDB

Building Your First MongoDB Application
Randall Hunt
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.
Speaker Bio: Randall is a HackNY mentor. He loves python, programming, space, and exploring NYC.

mongodb

August 02, 2012
Tweet

Transcript

  1. An Introduction
    With Randall Hunt
    Building Applications
    With MongoDB
    @jrhunt

    View Slide

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



    View Slide

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






    View Slide

  4. 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




    View Slide

  5. In Good Company

    View Slide

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



    View Slide

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

    View Slide

  8. 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" ]
    }

    View Slide

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

    View Slide

  10. 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

    View Slide

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

    View Slide

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

    View Slide

  13. 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"
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. 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" })

    View Slide

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

    View Slide

  19. .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" : { }
    }

    View Slide

  20. 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",
    ....

    View Slide

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

    View Slide

  22. 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"})

    View Slide

  23. 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]}})

    View Slide

  24. 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" })

    View Slide

  25. 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"},
    {.....}]
    }

    View Slide

  26. 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

    View Slide

  27. 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"}}}})

    View Slide

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



    View Slide

  29. 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

    View Slide

  30. Users
    checkins [] = ObjectId reference to check-in
    collection
    user1 = {
    name : "Randall",
    address : "[email protected]",
    ...
    checkins : [4b97e62bf1d8c7152c9ccb74,
    5a20e62bf1d8c736ab]
    }

    View Slide

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

    View Slide

  32. Checking In
    Insert check in object
    [checkin collection]
    Update ($push) user object
    [user collection]


    2 Operations

    View Slide

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

    View Slide

  34. 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

    View Slide

  35. 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)

    View Slide

  36. 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: [….., ….., …..]}
    })

    View Slide

  37. 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
    : )

    View Slide

  38. MongoDB
    Deployments

    View Slide

  39. Replica Sets
    Primary
    Secondary
    Secondary
    Primary
    Secondary
    Arbiter
    Primary
    Secondary
    Secondary
    Secondary
    Secondary

    View Slide

  40. 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

    View Slide

  41. Try it for yourself at
    try.mongodb.org

    View Slide

  42. ranman.com
    github.com/ranman
    @jrhunt
    We’re hiring!! Engineers, Sales, Evangelist, Marketing & Support
    Contact us at [email protected]
    Questions?
    download at mongodb.org

    View Slide