SlideShare a Scribd company logo
Building applications with MongoDB –
                       An introduction


                         Roger Bodamer
                       roger@10gen.com
                            @rogerb


http://mongodb.org
http://10gen.com
Today’s Talk
• Developing your first Web Application with MongoDB

• What is MongoDB, Platforms and availability
• Data Modeling, queries and geospatial queries

• Location bases App
• Example uses MongoDB Javascript shell
Why MongoDB
• Intrinsic support for agile development

• Super low latency access to your data
   – Very little CPU overhead

• No Additional caching layer required

• Built in Replication and Horizontal Scaling support
MongoDB
• Document Oriented Database
  – Data is stored in documents, not tables / relations

• MongoDB is Implemented in C++ for best performance
• Platforms 32/64 bit Windows Linux, Mac OS-X, FreeBSD,
  Solaris

• Language drivers for:
  –   Ruby / Ruby-on-Rails
  –   Java
  –   C#
  –   JavaScript
  –   C / C++
  –   Erlang Python, Perl others..... and much more ! ..
Design
• Want to build an app where users can check in to a
  location

• Leave notes or comments about that location

• Iterative Approach:
   – Decide requirements
   – Design documents
   – Rinse, repeat :-)
Requirements
• Locations
   – Need to store locations (Offices, Restaurants etc)
     • Want to be able to store name, address and tags
     • Maybe User Generated Content, i.e. tips / small notes ?

  – Want to be able to find other locations nearby
Requirements
• Locations
   – Need to store locations (Offices, Restaurants etc)
     • Want to be able to store name, address and tags
     • Maybe User Generated Content, i.e. tips / small notes ?

  – Want to be able to find other locations nearby

• Checkins
  – User should be able to ‘check in’ to a location
  – Want to be able to generate statistics
Terminology
RDBMS           Mongo
Table, View     Collection
Row(s)          JSON Document
Index           Index
Join            Embedded Document
Partition       Shard
Partition Key   Shard Key
Collections



loc1, loc2, loc3                 User1, User2



    Locations                      Users
JSON Sample Doc
 { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
   author : "roger",
   date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)",
   text : ”MongoSF",
   tags : [ ”San Francisco", ”MongoDB" ] }

Notes:
 - _id is unique, but can be anything you’d like
BSON
• JSON has powerful, but limited set of datatypes
   – Mongo extends datypes with Date, Int types, Id, …

• MongoDB stores data in BSON

• BSON is a binary representation of JSON
  – Optimized for performance and navigational abilities
  – Also compression
  – See bsonspec.org
Locations v1
location1= {
    name: "10gen East Coast”,
    address: ”134 5th Avenue 3rd Floor”,
    city: "New York”,
    zip: "10011”
}
Places v1
location1= {
    name: "10gen East Coast”,
    address: ”134 5th Avenue 3rd Floor”,
    city: "New York”,
    zip: "10011”
}


db.locations.find({zip:”10011”}).limit(10)
Places v2
location1 = {
    name: "10gen East Coast”,
    address: "17 West 18th Street 8th Floor”,
    city: "New York”,
    zip: "10011”,

    tags: [“business”, “mongodb”]
}
Places v2
location1 = {
    name: "10gen East Coast”,
    address: "17 West 18th Street 8th Floor”,
    city: "New York”,
    zip: "10011”,

    tags: [“business”, “mongodb”]
}

db.locations.find({zip:”10011”, tags:”business”})
Places v3
location1 = {
    name: "10gen East Coast”,
    address: "17 West 18th Street 8th Floor”,
    city: "New York”,
    zip: "10011”,

    tags: [“business”, “mongodb”],

    latlong: [40.0,72.0]
}
Places v3
location1 = {
    name: "10gen East Coast”,
    address: "17 West 18th Street 8th Floor”,
    city: "New York”,
    zip: "10011”,

    tags: [“business”, “cool place”],

    latlong: [40.0,72.0]
}

db.locations.ensureIndex({latlong:”2d”})
location1 = {
                    Places v3
    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.locations.ensureIndex({latlong:”2d”})
db.locations.find({latlong:{$near:[40,70]}})
location1 = {
                    Places v4
    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"},
        {.....},
         ]
}
Querying your Places
Creating your indexes
db.locations.ensureIndex({tags:1})
db.locations.ensureIndex({name:1})
db.locations.ensureIndex({latlong:”2d”})

Finding places:
db.locations.find({latlong:{$near:[40,70]}})

With regular expressions:
db.locations.find({name: /^typeaheadstring/)

By tag:
db.locations.find({tags: “business”})
Inserting and updating
                locations
Initial data load:
db.locations.insert(place1)


Using update to Add tips:
db.locations.update({name:"10gen HQ"},
   {$push :{tips:
           {user:"nosh", time:6/26/2010,
     tip:"stop by for office hours on
Wednesdays from 4-6"}}}}
Requirements
• Locations
   – Need to store locations (Offices, Restaurants etc)
     • Want to be able to store name, address and tags
     • Maybe User Generated Content, i.e. tips / small notes ?

  – Want to be able to find other locations nearby

• Checkins
  – User should be able to ‘check in’ to a location
  – Want to be able to generate statistics
Users
user1 = {
    name: “nosh”
    email: “nosh@10gen.com”,
    .
    .
    .
    checkins: [{ location: “10gen HQ”,
                  ts: 9/20/2010 10:12:00,
                  …},
                  …
               ]
}
Simple Stats
db.users.find({‘checkins.location’: “10gen HQ”)


db.checkins.find({‘checkins.location’: “10gen HQ”})
                 .sort({ts:-1}).limit(10)


db.checkins.find({‘checkins.location’: “10gen HQ”,
                 ts: {$gt: midnight}}).count()
Alternative
user1 = {
    name: “nosh”
    email: “nosh@10gen.com”,
    .
    .
    .
    checkins: [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab]
}



checkins [] = ObjectId reference to
locations collection
User Check in

Check-in = 2 ops
    read location to obtain location id
    Update ($push) location id to user object

Queries: find all locations where a user checked in:
     checkin_array = db.users.find({..},
                                     {checkins:true}).checkins

     db.location.find({_id:{$in: checkin_array}})
Unsharded Deployment
            •Configure as a replica set for automated
Primary
            failover
            •Async replication between nodes

            •Add more secondaries to scale reads
Secondary




Secondary
Sharded Deployment
                           MongoS




                                                      config

 Primary




Secondary



•Autosharding distributes data among two or more replica sets
  •Mongo Config Server(s) handles distribution & balancing
                 •Transparent to applications
Use Cases
 •RDBMS replacement for high-traffic web applications

        •Content Management-type applications

                  •Real-time analytics

               •High-speed data logging



Web 2.0, Media, SaaS, Gaming, Finance, Telecom,
 Healthcare
10Gen is hiring!
                       @mongodb

                     roger@10gen.com
                         @rogerb

http://mongodb.org
http://10gen.com
Ad

Recommended

MongoDB Schema Design
MongoDB Schema Design
MongoDB
 
Mongo DB schema design patterns
Mongo DB schema design patterns
joergreichert
 
Webinar: Schema Design
Webinar: Schema Design
MongoDB
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
Mike Friedman
 
Modeling Data in MongoDB
Modeling Data in MongoDB
lehresman
 
Building your first app with mongo db
Building your first app with mongo db
MongoDB
 
Schema Design by Example ~ MongoSF 2012
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in Documents
MongoDB
 
Building a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and Java
antoinegirbal
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documents
MongoDB
 
Schema Design with MongoDB
Schema Design with MongoDB
rogerbodamer
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
Matias Cascallares
 
MongoDB Schema Design
MongoDB Schema Design
Alex Litvinok
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best Practices
MongoDB
 
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
MongoDB
 
Agile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDB
Stennie Steneker
 
MongoDB Advanced Schema Design - Inboxes
MongoDB Advanced Schema Design - Inboxes
Jared Rosoff
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
Socialite, the Open Source Status Feed
Socialite, the Open Source Status Feed
MongoDB
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Catalog
hungarianhc
 
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
MongoDB
 
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB
 
Building a Social Network with MongoDB
Building a Social Network with MongoDB
Fred Chu
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDB
Marakana Inc.
 
Building Apps with MongoDB
Building Apps with MongoDB
Nate Abele
 
Data Modeling for the Real World
Data Modeling for the Real World
Mike Friedman
 
Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011
rogerbodamer
 
Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011
Steven Francia
 
Webinar: Building Your First Application with MongoDB
Webinar: Building Your First Application with MongoDB
MongoDB
 

More Related Content

What's hot (19)

Building a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and Java
antoinegirbal
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documents
MongoDB
 
Schema Design with MongoDB
Schema Design with MongoDB
rogerbodamer
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
Matias Cascallares
 
MongoDB Schema Design
MongoDB Schema Design
Alex Litvinok
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best Practices
MongoDB
 
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
MongoDB
 
Agile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDB
Stennie Steneker
 
MongoDB Advanced Schema Design - Inboxes
MongoDB Advanced Schema Design - Inboxes
Jared Rosoff
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
Socialite, the Open Source Status Feed
Socialite, the Open Source Status Feed
MongoDB
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Catalog
hungarianhc
 
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
MongoDB
 
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB
 
Building a Social Network with MongoDB
Building a Social Network with MongoDB
Fred Chu
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDB
Marakana Inc.
 
Building Apps with MongoDB
Building Apps with MongoDB
Nate Abele
 
Data Modeling for the Real World
Data Modeling for the Real World
Mike Friedman
 
Building a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and Java
antoinegirbal
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documents
MongoDB
 
Schema Design with MongoDB
Schema Design with MongoDB
rogerbodamer
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
Matias Cascallares
 
MongoDB Schema Design
MongoDB Schema Design
Alex Litvinok
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best Practices
MongoDB
 
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
MongoDB
 
Agile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDB
Stennie Steneker
 
MongoDB Advanced Schema Design - Inboxes
MongoDB Advanced Schema Design - Inboxes
Jared Rosoff
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
MongoDB
 
Socialite, the Open Source Status Feed
Socialite, the Open Source Status Feed
MongoDB
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Catalog
hungarianhc
 
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
MongoDB
 
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB
 
Building a Social Network with MongoDB
Building a Social Network with MongoDB
Fred Chu
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDB
Marakana Inc.
 
Building Apps with MongoDB
Building Apps with MongoDB
Nate Abele
 
Data Modeling for the Real World
Data Modeling for the Real World
Mike Friedman
 

Similar to Building web applications with mongo db presentation (20)

Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011
rogerbodamer
 
Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011
Steven Francia
 
Webinar: Building Your First Application with MongoDB
Webinar: Building Your First Application with MongoDB
MongoDB
 
First app online conf
First app online conf
MongoDB
 
Building Your First MongoDB Application
Building Your First MongoDB Application
Rick Copeland
 
Nosh slides mongodb web application - mongo philly 2011
Nosh slides mongodb web application - mongo philly 2011
MongoDB
 
Introduction to MongoDB
Introduction to MongoDB
Nosh Petigara
 
Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)
MongoDB
 
Building Applications with MongoDB - an Introduction
Building Applications with MongoDB - an Introduction
MongoDB
 
Building a web application with mongo db
Building a web application with mongo db
MongoDB
 
Building a Location-based platform with MongoDB from Zero.
Building a Location-based platform with MongoDB from Zero.
Ravi Teja
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
MongoDB
 
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
NETWAYS
 
Geoindexing with MongoDB
Geoindexing with MongoDB
leafnode
 
Webinar: Getting Started with MongoDB - Back to Basics
Webinar: Getting Started with MongoDB - Back to Basics
MongoDB
 
Using Spring with NoSQL databases (SpringOne China 2012)
Using Spring with NoSQL databases (SpringOne China 2012)
Chris Richardson
 
Real-time Location Based Social Discovery using MongoDB
Real-time Location Based Social Discovery using MongoDB
Fredrik Björk
 
Mongodb intro
Mongodb intro
christkv
 
Webinar : Premiers pas avec MongoDB - Back to Basics
Webinar : Premiers pas avec MongoDB - Back to Basics
MongoDB
 
Mongo db intro new
Mongo db intro new
Abhinav Dhasmana
 
Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011
rogerbodamer
 
Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011
Steven Francia
 
Webinar: Building Your First Application with MongoDB
Webinar: Building Your First Application with MongoDB
MongoDB
 
First app online conf
First app online conf
MongoDB
 
Building Your First MongoDB Application
Building Your First MongoDB Application
Rick Copeland
 
Nosh slides mongodb web application - mongo philly 2011
Nosh slides mongodb web application - mongo philly 2011
MongoDB
 
Introduction to MongoDB
Introduction to MongoDB
Nosh Petigara
 
Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)
MongoDB
 
Building Applications with MongoDB - an Introduction
Building Applications with MongoDB - an Introduction
MongoDB
 
Building a web application with mongo db
Building a web application with mongo db
MongoDB
 
Building a Location-based platform with MongoDB from Zero.
Building a Location-based platform with MongoDB from Zero.
Ravi Teja
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
MongoDB
 
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
NETWAYS
 
Geoindexing with MongoDB
Geoindexing with MongoDB
leafnode
 
Webinar: Getting Started with MongoDB - Back to Basics
Webinar: Getting Started with MongoDB - Back to Basics
MongoDB
 
Using Spring with NoSQL databases (SpringOne China 2012)
Using Spring with NoSQL databases (SpringOne China 2012)
Chris Richardson
 
Real-time Location Based Social Discovery using MongoDB
Real-time Location Based Social Discovery using MongoDB
Fredrik Björk
 
Mongodb intro
Mongodb intro
christkv
 
Webinar : Premiers pas avec MongoDB - Back to Basics
Webinar : Premiers pas avec MongoDB - Back to Basics
MongoDB
 
Ad

More from Murat Çakal (9)

REST vs. SOAP
REST vs. SOAP
Murat Çakal
 
Cassandra NoSQL
Cassandra NoSQL
Murat Çakal
 
Scaling web applications with cassandra presentation
Scaling web applications with cassandra presentation
Murat Çakal
 
Mongodb open source_high_performance_database
Mongodb open source_high_performance_database
Murat Çakal
 
Wmware NoSQL
Wmware NoSQL
Murat Çakal
 
Trouble with nosql_dbs
Trouble with nosql_dbs
Murat Çakal
 
NoSql databases
NoSql databases
Murat Çakal
 
RDBMS vs NoSQL
RDBMS vs NoSQL
Murat Çakal
 
No sql
No sql
Murat Çakal
 
Scaling web applications with cassandra presentation
Scaling web applications with cassandra presentation
Murat Çakal
 
Mongodb open source_high_performance_database
Mongodb open source_high_performance_database
Murat Çakal
 
Trouble with nosql_dbs
Trouble with nosql_dbs
Murat Çakal
 
Ad

Building web applications with mongo db presentation

  • 1. Building applications with MongoDB – An introduction Roger Bodamer [email protected] @rogerb http://mongodb.org http://10gen.com
  • 2. Today’s Talk • Developing your first Web Application with MongoDB • What is MongoDB, Platforms and availability • Data Modeling, queries and geospatial queries • Location bases App • Example uses MongoDB Javascript shell
  • 3. Why MongoDB • Intrinsic support for agile development • Super low latency access to your data – Very little CPU overhead • No Additional caching layer required • Built in Replication and Horizontal Scaling support
  • 4. MongoDB • Document Oriented Database – Data is stored in documents, not tables / relations • MongoDB is Implemented in C++ for best performance • Platforms 32/64 bit Windows Linux, Mac OS-X, FreeBSD, Solaris • Language drivers for: – Ruby / Ruby-on-Rails – Java – C# – JavaScript – C / C++ – Erlang Python, Perl others..... and much more ! ..
  • 5. Design • Want to build an app where users can check in to a location • Leave notes or comments about that location • Iterative Approach: – Decide requirements – Design documents – Rinse, repeat :-)
  • 6. Requirements • Locations – Need to store locations (Offices, Restaurants etc) • Want to be able to store name, address and tags • Maybe User Generated Content, i.e. tips / small notes ? – Want to be able to find other locations nearby
  • 7. Requirements • Locations – Need to store locations (Offices, Restaurants etc) • Want to be able to store name, address and tags • Maybe User Generated Content, i.e. tips / small notes ? – Want to be able to find other locations nearby • Checkins – User should be able to ‘check in’ to a location – Want to be able to generate statistics
  • 8. Terminology RDBMS Mongo Table, View Collection Row(s) JSON Document Index Index Join Embedded Document Partition Shard Partition Key Shard Key
  • 9. Collections loc1, loc2, loc3 User1, User2 Locations Users
  • 10. JSON Sample Doc { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : ”MongoSF", tags : [ ”San Francisco", ”MongoDB" ] } Notes: - _id is unique, but can be anything you’d like
  • 11. BSON • JSON has powerful, but limited set of datatypes – Mongo extends datypes with Date, Int types, Id, … • MongoDB stores data in BSON • BSON is a binary representation of JSON – Optimized for performance and navigational abilities – Also compression – See bsonspec.org
  • 12. Locations v1 location1= { name: "10gen East Coast”, address: ”134 5th Avenue 3rd Floor”, city: "New York”, zip: "10011” }
  • 13. Places v1 location1= { name: "10gen East Coast”, address: ”134 5th Avenue 3rd Floor”, city: "New York”, zip: "10011” } db.locations.find({zip:”10011”}).limit(10)
  • 14. Places v2 location1 = { name: "10gen East Coast”, address: "17 West 18th Street 8th Floor”, city: "New York”, zip: "10011”, tags: [“business”, “mongodb”] }
  • 15. Places v2 location1 = { name: "10gen East Coast”, address: "17 West 18th Street 8th Floor”, city: "New York”, zip: "10011”, tags: [“business”, “mongodb”] } db.locations.find({zip:”10011”, tags:”business”})
  • 16. Places v3 location1 = { name: "10gen East Coast”, address: "17 West 18th Street 8th Floor”, city: "New York”, zip: "10011”, tags: [“business”, “mongodb”], latlong: [40.0,72.0] }
  • 17. Places v3 location1 = { name: "10gen East Coast”, address: "17 West 18th Street 8th Floor”, city: "New York”, zip: "10011”, tags: [“business”, “cool place”], latlong: [40.0,72.0] } db.locations.ensureIndex({latlong:”2d”})
  • 18. location1 = { Places v3 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.locations.ensureIndex({latlong:”2d”}) db.locations.find({latlong:{$near:[40,70]}})
  • 19. location1 = { Places v4 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"}, {.....}, ] }
  • 20. Querying your Places Creating your indexes db.locations.ensureIndex({tags:1}) db.locations.ensureIndex({name:1}) db.locations.ensureIndex({latlong:”2d”}) Finding places: db.locations.find({latlong:{$near:[40,70]}}) With regular expressions: db.locations.find({name: /^typeaheadstring/) By tag: db.locations.find({tags: “business”})
  • 21. Inserting and updating locations Initial data load: db.locations.insert(place1) Using update to Add tips: db.locations.update({name:"10gen HQ"}, {$push :{tips: {user:"nosh", time:6/26/2010, tip:"stop by for office hours on Wednesdays from 4-6"}}}}
  • 22. Requirements • Locations – Need to store locations (Offices, Restaurants etc) • Want to be able to store name, address and tags • Maybe User Generated Content, i.e. tips / small notes ? – Want to be able to find other locations nearby • Checkins – User should be able to ‘check in’ to a location – Want to be able to generate statistics
  • 23. Users user1 = { name: “nosh” email: “[email protected]”, . . . checkins: [{ location: “10gen HQ”, ts: 9/20/2010 10:12:00, …}, … ] }
  • 24. Simple Stats db.users.find({‘checkins.location’: “10gen HQ”) db.checkins.find({‘checkins.location’: “10gen HQ”}) .sort({ts:-1}).limit(10) db.checkins.find({‘checkins.location’: “10gen HQ”, ts: {$gt: midnight}}).count()
  • 25. Alternative user1 = { name: “nosh” email: “[email protected]”, . . . checkins: [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab] } checkins [] = ObjectId reference to locations collection
  • 26. User Check in Check-in = 2 ops read location to obtain location id Update ($push) location id to user object Queries: find all locations where a user checked in: checkin_array = db.users.find({..}, {checkins:true}).checkins db.location.find({_id:{$in: checkin_array}})
  • 27. Unsharded Deployment •Configure as a replica set for automated Primary failover •Async replication between nodes •Add more secondaries to scale reads Secondary Secondary
  • 28. Sharded Deployment MongoS config Primary Secondary •Autosharding distributes data among two or more replica sets •Mongo Config Server(s) handles distribution & balancing •Transparent to applications
  • 29. Use Cases •RDBMS replacement for high-traffic web applications •Content Management-type applications •Real-time analytics •High-speed data logging Web 2.0, Media, SaaS, Gaming, Finance, Telecom, Healthcare
  • 30. 10Gen is hiring! @mongodb [email protected] @rogerb http://mongodb.org http://10gen.com