SlideShare a Scribd company logo
Building Your First MongoDB Application
Ross Lawley
Python Engineer @ 10gen
Web developer since 1999
Passionate about open source
Agile methodology
email: ross@10gen.com
twitter: RossC0
Today's Talk
• Quick introduction to NoSQL
• Some Background about mongoDB
• Using mongoDB, general querying and usage
• Building your first app, creating a location-based
app
• Data modelling in mongoDB, queries, geospatial,
updates and map reduce (examples work in
mongoDB JS shell)
What is NoSQL?
Key / Value Column Graph Document
Key-Value Stores
•A mapping from a key to a value
•The store doesn't know anything about the the key
or value
•The store doesn't know anything about the insides
of the value
•Operations
• Set, get, or delete a key-value pair
Column-Oriented Stores
•Like a relational store, but flipped around: all data
for a column is kept together
• An index provides a means to get a column value for a
record
•Operations:
• Get, insert, delete records; updating fields
• Streaming column data in and out of Hadoop
Graph Databases
•Stores vertex-to-vertex edges
•Operations:
• Getting and setting edges
• Sometimes possible to annotate vertices or edges
• Query languages support finding paths between
vertices, subject to various constraints
Document Stores
•The store is a container for documents
• Documents are made up of named fields
• Fields may or may not have type definitions
• e.g. XSDs for XML stores, vs. schema-less JSON stores
•Can create "secondary indexes"
• These provide the ability to query on any document field(s)
•Operations:
• Insert and delete documents
• Update fields within documents
What is ?
MongoDB is a scalable, high-performance,
open source NoSQL database.
•Document-oriented storage
•Full Index Support
•Querying
•Fast In-Place Updates
•Replication & High Availability
•Auto-Sharding
•Map/Reduce
•GridFS
Database Landscape
depth of functionality
scalability&performance
memcached
key/value
RDBMS
History
•First release – February 2009
•v1.0 - August 2009
•v1.2 - December 2009 – MapReduce, ++
•v1.4 - March 2010 – Concurrency, Geo
•V1.6 - August 2010 – Sharding, Replica Sets
•V1.8 – March 2011 – Journaling, Geosphere
•V2.0 - Sep 2011 – V1 Indexes, Concurrency
•V2.2 - Soon - Aggregation, Concurrency
• Company behind mongoDB
– (A)GPL license, own copyrights, engineering team
– support, consulting, commercial license
• Management
– Google/DoubleClick, Oracle, Apple, NetApp
– Funding: Sequoia, Union Square, Flybridge
– Offices in NYC, Palo Alto, London, Dublin
– 100+ employees
Where can you use it?
MongoDB is Implemented in C++
• Platforms 32/64 bit Windows, Linux, Mac OS-X,
FreeBSD, Solaris
Drivers are available in many languages
10gen supported
• C, C# (.Net), C++, Erlang, Haskell, Java, JavaScript,
Perl, PHP, Python, Ruby, Scala
Community supported
• Clojure, ColdFusion, F#, Go, Groovy, Lua, R ...
http://www.mongodb.org/display/DOCS/Drivers
Why use 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
Terminology
RDBMS MongoDB
Table Collection
Row(s) JSON Document
Index Index
Join Embedding & Linking
Partition Shard
Partition Key Shard Key
> p = { author: "Ross",
date: new Date(),
text: "About MongoDB...",
tags: ["tech", "databases"]}
> db.posts.save(p)
Documents
Blog Post Document
> db.posts.find()
{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author : "Ross",
date : ISODate("2012-02-02T11:52:27.442Z"),
text : "About MongoDB...",
tags : [ "tech", "databases" ] }
Querying
Notes:
_id is unique, but can be anything you'd like
Introducing BSON
JSON has powerful, but limited set of datatypes
• arrays, objects, strings, numbers and null
BSON is a binary representation of JSON
• Adds extra dataypes with Date, Int types, Id, …
• Optimized for performance and navigational abilities
• And compression
MongoDB sends and stores data in BSON
BSON
http://bsonspec.org
// find posts with any tags
> db.posts.find({tags: {$exists: true }})
// find posts matching a regular expression
> db.posts.find({author: /^ro*/i })
// count posts by author
> db.posts.find({author: 'Ross'}).count()
Query Operators
Conditional Operators
- $all, $exists, $mod, $ne, $in, $nin, $nor, $or, $size, $type
- $lt, $lte, $gt, $gte
// 1 means ascending, -1 means descending
> db.posts.ensureIndex({author: 1})
> db.posts.findOne({author: 'Ross'})
{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author: "Ross", ... }
Secondary Indexes
Create index on any Field in Document
// 1 means ascending, -1 means descending
> db.posts.ensureIndex({author: 1, ts: -1})
> db.posts.find({author: 'Ross'}).sort({ts: -1})
[{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author: "Ross", ...},
{ _id : ObjectId("4f61d325c496820ceba84124"),
author: "Ross", ...}]
Compound Indexes
Create index on multiple fields in a Document
> db.posts.find({"author": 'Ross'}).explain()
{
	 "cursor" : "BtreeCursor author_1",
	 "nscanned" : 1,
	 "nscannedObjects" : 1,
	 "n" : 1,
	 "millis" : 0,
	 "indexBounds" : {
	 	 "author" : [
	 	 	 [
	 	 	 	 "Ross",
	 	 	 	 "Ross"
	 	 	 ]
	 	 ]
	 }
}
Examine the query plan
// Create a comment
> new_comment = { author: "Fred",
date: new Date(),
text: "Best Post Ever!"}
// Add to post
> db.posts.update({ _id: "..." },
	 	 	 {"$push": {comments: new_comment},
"$inc": {comments_count: 1}
});
Atomic Operations
$set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $bit
{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author : "Ross",
date : "Thu Feb 02 2012 11:50:01",
text : "About MongoDB...",
tags : [ "tech", "databases" ],
comments : [{
	 	 author : "Fred",
	 	 date : "Fri Feb 03 2012 13:23:11",
	 	 text : "Best Post Ever!"
	 }],
comment_count : 1
}
Nested Documents
{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
author : "Ross",
date : "Thu Feb 02 2012 11:50:01",
text : "About MongoDB...",
tags : [ "tech", "databases" ],
comments : [{
	 	 author : "Fred",
	 	 date : "Fri Feb 03 2012 13:23:11",
	 	 text : "Best Post Ever!"
	 }],
comment_count : 1
}
Nested Documents
// Index nested documents
> db.posts.ensureIndex("comments.author": 1)
> db.posts.find({"comments.author": "Fred"})
// Index on tags (multi-key index)
> db.posts.ensureIndex( tags: 1)
> db.posts.find( { tags: "tech" } )
Secondary Indexes
Geo
• Geo-spatial queries
• Require a geo index
• Find points near a given point
• Find points within a polygon/sphere
// geospatial index
> db.posts.ensureIndex({"author.location": "2d"})
> db.posts.find({ "author.location" :
{ $near : [22, 42] }})
Map Reduce
The caller provides map and reduce functions written
in JavaScript
// Emit each tag
> map = "this['tags'].forEach(
function(item) {emit(item, 1);}
);"
// Calculate totals
> reduce = "function(key, values) {
var total = 0;
var valuesSize = values.length;
for (var i=0; i < valuesSize; i++) {
total += parseInt(values[i], 10);
}
return total;
};
// run the map reduce
> db.posts.mapReduce(map, reduce, {"out": { inline : 1}});
{
	 "results" : [
	 	 {"_id" : "databases", "value" : 1},
	 	 {"_id" : "tech", "value" : 1 }
	 ],
	 "timeMillis" : 1,
	 "counts" : {
	 	 "input" : 1,
	 	 "emit" : 2,
	 	 "reduce" : 0,
	 	 "output" : 2
	 },
	 "ok" : 1,
}
Map Reduce
// Count tags
> agg = db.posts.aggregate(
{$unwind: "$tags"},
{$group : {_id : "$tags",
count : {$sum: 1}}}
)
> agg.result
[{"_id": "databases", "count": 1},
{"_id": "tech", "count": 1}]
Aggregation - coming in 2.2
// (Python) Create a new instance of GridFS
>>> fs = gridfs.GridFS(db)
// Save file to mongo
>>> my_image = open('my_image.jpg', 'r')
>>> file_id = fs.put(my_image)
// Read file
>>> fs.get(file_id).read()
GridFS
Save files in mongoDB
Stream data back to the client
Building Your First MongoDB App
• 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
"As a user I want to be able to find other locations nearby"
• Need to store locations (Offices, Restaurants etc)
• name, address, tags
• coordinates
• user generated content e.g. tips / notes
Requirements
"As a user I want to be able to 'checkin' to a location"
Checkins
• User should be able to 'check in' to a location
• Want to be able to generate statistics:
• Recent checkins
• Popular locations
> location_1 = {
name: "Holiday Inn",
address: "Engelhardsgasse 12",
city: "Nürnberg ",
post_code: "D-90402"
}
Locations v1
> location_1 = {
name: "Holiday Inn",
address: "Engelhardsgasse 12",
city: "Nürnberg ",
post_code: "D-90402"
}
> db.locations.save(location_1)
> db.locations.find({name: "Holiday Inn"})
Locations v1
> location_2 = {
name: "Holiday Inn",
address: "Engelhardsgasse 12",
city: "Nürnberg ",
post_code: "D-90402",
tags: ["hotel", "conference", "mongodb"]
}
Locations v2
> location_2 = {
name: "Holiday Inn",
address: "Engelhardsgasse 12",
city: "Nürnberg ",
post_code: "D-90402",
tags: ["hotel", "conference", "mongodb"]
}
> db.locations.ensureIndex({tags: 1})
> db.locations.find({tags: "hotel"})
Locations v2
> location_3 = {
name: "Holiday Inn",
address: "Engelhardsgasse 12",
city: "Nürnberg ",
post_code: "D-90402",
tags: ["hotel", "conference", "mongodb"],
lat_long: [52.5184, 13.387]
}
Locations v3
> location_3 = {
name: "Holiday Inn",
address: "Engelhardsgasse 12",
city: "Nürnberg ",
post_code: "D-90402",
tags: ["hotel", "conference", "mongodb"],
lat_long: [52.5184, 13.387]
}
> db.locations.ensureIndex({lat_long: "2d"})
> db.locations.find({lat_long: {$near:[52.53, 13.4]}})
Locations v3
// creating your indexes:
> db.locations.ensureIndex({name: 1})
> db.locations.ensureIndex({tags: 1})
> db.locations.ensureIndex({lat_long: "2d"})
// with regular expressions:
> db.locations.find({name: /^holid/i})
// by tag:
> db.locations.find({tag: "hotel"})
// finding places:
> db.locations.find({lat_long: {$near:[52.53, 13.4]}})
Finding locations
// initial data load:
> db.locations.insert(location_3)
// adding a tip with update:
> db.locations.update(
{name: "Holiday Inn"},
{$push: {
tips: {
user: "Ross",
date: ISODate("05-04-2012"),
tip: "Check out my replication talk later!"}
}})
Inserting locations - adding tips
> db.locations.findOne()
{ _id : ObjectId("5c4ba5c0672c685e5e8aabf3"),
name: "Holiday Inn",
address: "Engelhardsgasse 12",
city: "Nürnberg ",
post_code: "D-90402",
tags: ["hotel", "conference", "mongodb"],
lat_long: [52.5184, 13.387],
tips:[{
user: "Ross",
date: ISODate("05-04-2012"),
tip: "Check out my replication talk later!"
}]
}
Tips added
Requirements
"As a user I want to be able to find other locations nearby"
Need to store locations (Offices, Restaurants etc)
• name, address, tags
• coordinates
• user generated content e.g. tips / notes
"As a user I want to be able to 'checkin' to a location"
Checkins
• User should be able to 'check in' to a location
• Want to be able to generate statistics:
• Recent checkins
• Popular locations
> user_1 = {
_id: "ross@10gen.com",
name: "Ross",
twitter: "RossC0",
checkins: [
{location: "Holiday Inn", ts: "25/04/2012"},
{location: "Munich Airport", ts: "24/04/2012"}
]
}
Users and Checkins
// find all users who've checked in here:
> db.users.find({"checkins.location":"Holiday Inn"})
Simple Stats
// find all users who've checked in here:
> db.users.find({"checkins.location":"Holiday Inn"})
// Can't find the last 10 checkins easily
> db.users.find({"checkins.location":"Holiday Inn"})
.sort({"checkins.ts": -1}).limit(10)
Schema hard to query for stats.
Simple Stats
> user_2 = {
_id: "ross@10gen.com",
name: "Ross",
twitter: "RossC0",
}
> checkin_1 = {
location: location_id,
user: user_id,
ts: ISODate("05-04-2012")
}
> db.checkins.find({user: user_id})
User and Checkins v2
// find all users who've checked in here:
> loc = db.locations.findOne({"name":"Holiday Inn"})
> checkins = db.checkins.find({location: loc._id})
> u_ids = checkins.map(function(c){return c.user})
> users = db.users.find({_id: {$in: u_ids}})
// find the last 10 checkins here:
> db.checkins.find({location: loc._id})
.sort({ts: -1}).limit(10)
// count how many checked in today:
> db.checkins.find({location: loc._id,
ts: {$gt: midnight}}
).count()
Simple Stats
// Find most popular locations
> map_func = function() {
emit(this.location, 1);
}
> reduce_func = function(key, values) {
return Array.sum(values);
}
> db.checkins.mapReduce(map_func, reduce_func,
{query: {ts: {$gt: now_minus_3_hrs}},
out: "result"})
> db.result.findOne()
{"_id": "Holiday Inn", "value" : 99}
Map Reduce
// Find most popular locations
> agg = db.checkins.aggregate(
{$match: {ts: {$gt: now_minus_3_hrs}}},
{$group: {_id: "$location",
value: {$sum: 1}}}
)
> agg.result
[{"_id": "Holiday Inn", "value" : 17}]
Aggregation
P
Deployment
• Single server
- need a strong backup plan
Deployment
• Single server
- need a strong backup plan
• Replica sets
- High availability
- Automatic failover
P
P S S
Deployment
• Single server
- need a strong backup plan
• Replica sets
- High availability
- Automatic failover
• Sharded
- Horizontally scale
- Auto balancing P S S
P S S
P
P S S
MongoDB Use Cases
• Archiving
• Content Management
• Ecommerce
• Finance
• Gaming
• Government
• Metadata Storage
• News & Media
• Online Advertising
• Online Collaboration
• Real-time stats/analytics
• Social Networks
• Telecommunications
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
Any Questions?
@mongodb
conferences, appearances, and meetups
http://www.10gen.com/events
http://bit.ly/mongofb
Facebook | Twitter | LinkedIn
http://linkd.in/joinmongo
download at mongodb.org
support, training, and this talk brought to you by

More Related Content

PDF
Build your first MongoDB App in Ruby @ StrangeLoop 2013
KEY
PPTX
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
PPTX
Mongo db queries
PPT
Building web applications with mongo db presentation
PPTX
Webinar: Schema Design
PPTX
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
KEY
Modeling Data in MongoDB
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Mongo db queries
Building web applications with mongo db presentation
Webinar: Schema Design
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
Modeling Data in MongoDB

What's hot (20)

PDF
Building your first app with mongo db
PDF
Webinar: Working with Graph Data in MongoDB
KEY
Practical Ruby Projects With Mongo Db
KEY
MongoDB and hadoop
PDF
Building Apps with MongoDB
KEY
OSCON 2012 MongoDB Tutorial
PPTX
Indexing Strategies to Help You Scale
PPTX
Webinar: Getting Started with MongoDB - Back to Basics
ODP
MongoDB : The Definitive Guide
PDF
Introduction to MongoDB
PDF
MongoDB Basics
PPT
MongoDB at the Silicon Valley iPhone and iPad Developers' Meetup
PDF
PgREST: Node.js in the Database
PPTX
MongoDB Schema Design: Four Real-World Examples
PDF
Mongo DB schema design patterns
KEY
MongoDB at GUL
PPTX
Back to Basics Webinar 1: Introduction to NoSQL
PDF
Agile Schema Design: An introduction to MongoDB
PPTX
PPT
Introduction to MongoDB
Building your first app with mongo db
Webinar: Working with Graph Data in MongoDB
Practical Ruby Projects With Mongo Db
MongoDB and hadoop
Building Apps with MongoDB
OSCON 2012 MongoDB Tutorial
Indexing Strategies to Help You Scale
Webinar: Getting Started with MongoDB - Back to Basics
MongoDB : The Definitive Guide
Introduction to MongoDB
MongoDB Basics
MongoDB at the Silicon Valley iPhone and iPad Developers' Meetup
PgREST: Node.js in the Database
MongoDB Schema Design: Four Real-World Examples
Mongo DB schema design patterns
MongoDB at GUL
Back to Basics Webinar 1: Introduction to NoSQL
Agile Schema Design: An introduction to MongoDB
Introduction to MongoDB
Ad

Similar to OSDC 2012 | Building a first application on MongoDB by Ross Lawley (20)

KEY
Mongodb intro
PDF
Introduction to MongoDB
PDF
10gen MongoDB Video Presentation at WebGeek DevCup
PPTX
Webinar: General Technical Overview of MongoDB for Dev Teams
PPTX
MongoDB by Emroz sardar.
PPT
Introduction to MongoDB
PDF
MongoDB.pdf
PPT
9. Document Oriented Databases
PPT
Mongodb Training Tutorial in Bangalore
PPT
Building Your First MongoDB App ~ Metadata Catalog
PPT
mongodb-120401144140-phpapp01 claud camputing
KEY
MongoDB at CodeMash 2.0.1.0
PPTX
Intro to mongodb mongouk jun2010
PDF
Learn Learn how to build your mobile back-end with MongoDB
KEY
2012 phoenix mug
PDF
Building Your First MongoDB Application
KEY
MongoDB Strange Loop 2009
PDF
MongoDB NoSQL database a deep dive -MyWhitePaper
PPT
MongoDB Pros and Cons
PPTX
Webinar: Building Your First MongoDB App
Mongodb intro
Introduction to MongoDB
10gen MongoDB Video Presentation at WebGeek DevCup
Webinar: General Technical Overview of MongoDB for Dev Teams
MongoDB by Emroz sardar.
Introduction to MongoDB
MongoDB.pdf
9. Document Oriented Databases
Mongodb Training Tutorial in Bangalore
Building Your First MongoDB App ~ Metadata Catalog
mongodb-120401144140-phpapp01 claud camputing
MongoDB at CodeMash 2.0.1.0
Intro to mongodb mongouk jun2010
Learn Learn how to build your mobile back-end with MongoDB
2012 phoenix mug
Building Your First MongoDB Application
MongoDB Strange Loop 2009
MongoDB NoSQL database a deep dive -MyWhitePaper
MongoDB Pros and Cons
Webinar: Building Your First MongoDB App
Ad

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PPTX
history of c programming in notes for students .pptx
PPTX
L1 - Introduction to python Backend.pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPT
Introduction Database Management System for Course Database
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
top salesforce developer skills in 2025.pdf
PPTX
ai tools demonstartion for schools and inter college
PPTX
Transform Your Business with a Software ERP System
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Operating system designcfffgfgggggggvggggggggg
Nekopoi APK 2025 free lastest update
history of c programming in notes for students .pptx
L1 - Introduction to python Backend.pptx
CHAPTER 2 - PM Management and IT Context
How to Choose the Right IT Partner for Your Business in Malaysia
PTS Company Brochure 2025 (1).pdf.......
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
2025 Textile ERP Trends: SAP, Odoo & Oracle
Introduction Database Management System for Course Database
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo POS Development Services by CandidRoot Solutions
top salesforce developer skills in 2025.pdf
ai tools demonstartion for schools and inter college
Transform Your Business with a Software ERP System
Design an Analysis of Algorithms I-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
Operating system designcfffgfgggggggvggggggggg

OSDC 2012 | Building a first application on MongoDB by Ross Lawley

  • 1. Building Your First MongoDB Application
  • 2. Ross Lawley Python Engineer @ 10gen Web developer since 1999 Passionate about open source Agile methodology email: ross@10gen.com twitter: RossC0
  • 3. Today's Talk • Quick introduction to NoSQL • Some Background about mongoDB • Using mongoDB, general querying and usage • Building your first app, creating a location-based app • Data modelling in mongoDB, queries, geospatial, updates and map reduce (examples work in mongoDB JS shell)
  • 4. What is NoSQL? Key / Value Column Graph Document
  • 5. Key-Value Stores •A mapping from a key to a value •The store doesn't know anything about the the key or value •The store doesn't know anything about the insides of the value •Operations • Set, get, or delete a key-value pair
  • 6. Column-Oriented Stores •Like a relational store, but flipped around: all data for a column is kept together • An index provides a means to get a column value for a record •Operations: • Get, insert, delete records; updating fields • Streaming column data in and out of Hadoop
  • 7. Graph Databases •Stores vertex-to-vertex edges •Operations: • Getting and setting edges • Sometimes possible to annotate vertices or edges • Query languages support finding paths between vertices, subject to various constraints
  • 8. Document Stores •The store is a container for documents • Documents are made up of named fields • Fields may or may not have type definitions • e.g. XSDs for XML stores, vs. schema-less JSON stores •Can create "secondary indexes" • These provide the ability to query on any document field(s) •Operations: • Insert and delete documents • Update fields within documents
  • 9. What is ? MongoDB is a scalable, high-performance, open source NoSQL database. •Document-oriented storage •Full Index Support •Querying •Fast In-Place Updates •Replication & High Availability •Auto-Sharding •Map/Reduce •GridFS
  • 10. Database Landscape depth of functionality scalability&performance memcached key/value RDBMS
  • 11. History •First release – February 2009 •v1.0 - August 2009 •v1.2 - December 2009 – MapReduce, ++ •v1.4 - March 2010 – Concurrency, Geo •V1.6 - August 2010 – Sharding, Replica Sets •V1.8 – March 2011 – Journaling, Geosphere •V2.0 - Sep 2011 – V1 Indexes, Concurrency •V2.2 - Soon - Aggregation, Concurrency
  • 12. • Company behind mongoDB – (A)GPL license, own copyrights, engineering team – support, consulting, commercial license • Management – Google/DoubleClick, Oracle, Apple, NetApp – Funding: Sequoia, Union Square, Flybridge – Offices in NYC, Palo Alto, London, Dublin – 100+ employees
  • 13. Where can you use it? MongoDB is Implemented in C++ • Platforms 32/64 bit Windows, Linux, Mac OS-X, FreeBSD, Solaris Drivers are available in many languages 10gen supported • C, C# (.Net), C++, Erlang, Haskell, Java, JavaScript, Perl, PHP, Python, Ruby, Scala Community supported • Clojure, ColdFusion, F#, Go, Groovy, Lua, R ... http://www.mongodb.org/display/DOCS/Drivers
  • 14. Why use 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
  • 15. Terminology RDBMS MongoDB Table Collection Row(s) JSON Document Index Index Join Embedding & Linking Partition Shard Partition Key Shard Key
  • 16. > p = { author: "Ross", date: new Date(), text: "About MongoDB...", tags: ["tech", "databases"]} > db.posts.save(p) Documents Blog Post Document
  • 17. > db.posts.find() { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Ross", date : ISODate("2012-02-02T11:52:27.442Z"), text : "About MongoDB...", tags : [ "tech", "databases" ] } Querying Notes: _id is unique, but can be anything you'd like
  • 18. Introducing BSON JSON has powerful, but limited set of datatypes • arrays, objects, strings, numbers and null BSON is a binary representation of JSON • Adds extra dataypes with Date, Int types, Id, … • Optimized for performance and navigational abilities • And compression MongoDB sends and stores data in BSON
  • 20. // find posts with any tags > db.posts.find({tags: {$exists: true }}) // find posts matching a regular expression > db.posts.find({author: /^ro*/i }) // count posts by author > db.posts.find({author: 'Ross'}).count() Query Operators Conditional Operators - $all, $exists, $mod, $ne, $in, $nin, $nor, $or, $size, $type - $lt, $lte, $gt, $gte
  • 21. // 1 means ascending, -1 means descending > db.posts.ensureIndex({author: 1}) > db.posts.findOne({author: 'Ross'}) { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author: "Ross", ... } Secondary Indexes Create index on any Field in Document
  • 22. // 1 means ascending, -1 means descending > db.posts.ensureIndex({author: 1, ts: -1}) > db.posts.find({author: 'Ross'}).sort({ts: -1}) [{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author: "Ross", ...}, { _id : ObjectId("4f61d325c496820ceba84124"), author: "Ross", ...}] Compound Indexes Create index on multiple fields in a Document
  • 23. > db.posts.find({"author": 'Ross'}).explain() { "cursor" : "BtreeCursor author_1", "nscanned" : 1, "nscannedObjects" : 1, "n" : 1, "millis" : 0, "indexBounds" : { "author" : [ [ "Ross", "Ross" ] ] } } Examine the query plan
  • 24. // Create a comment > new_comment = { author: "Fred", date: new Date(), text: "Best Post Ever!"} // Add to post > db.posts.update({ _id: "..." }, {"$push": {comments: new_comment}, "$inc": {comments_count: 1} }); Atomic Operations $set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $bit
  • 25. { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Ross", date : "Thu Feb 02 2012 11:50:01", text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [{ author : "Fred", date : "Fri Feb 03 2012 13:23:11", text : "Best Post Ever!" }], comment_count : 1 } Nested Documents
  • 26. { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Ross", date : "Thu Feb 02 2012 11:50:01", text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [{ author : "Fred", date : "Fri Feb 03 2012 13:23:11", text : "Best Post Ever!" }], comment_count : 1 } Nested Documents
  • 27. // Index nested documents > db.posts.ensureIndex("comments.author": 1) > db.posts.find({"comments.author": "Fred"}) // Index on tags (multi-key index) > db.posts.ensureIndex( tags: 1) > db.posts.find( { tags: "tech" } ) Secondary Indexes
  • 28. Geo • Geo-spatial queries • Require a geo index • Find points near a given point • Find points within a polygon/sphere // geospatial index > db.posts.ensureIndex({"author.location": "2d"}) > db.posts.find({ "author.location" : { $near : [22, 42] }})
  • 29. Map Reduce The caller provides map and reduce functions written in JavaScript // Emit each tag > map = "this['tags'].forEach( function(item) {emit(item, 1);} );" // Calculate totals > reduce = "function(key, values) { var total = 0; var valuesSize = values.length; for (var i=0; i < valuesSize; i++) { total += parseInt(values[i], 10); } return total; };
  • 30. // run the map reduce > db.posts.mapReduce(map, reduce, {"out": { inline : 1}}); { "results" : [ {"_id" : "databases", "value" : 1}, {"_id" : "tech", "value" : 1 } ], "timeMillis" : 1, "counts" : { "input" : 1, "emit" : 2, "reduce" : 0, "output" : 2 }, "ok" : 1, } Map Reduce
  • 31. // Count tags > agg = db.posts.aggregate( {$unwind: "$tags"}, {$group : {_id : "$tags", count : {$sum: 1}}} ) > agg.result [{"_id": "databases", "count": 1}, {"_id": "tech", "count": 1}] Aggregation - coming in 2.2
  • 32. // (Python) Create a new instance of GridFS >>> fs = gridfs.GridFS(db) // Save file to mongo >>> my_image = open('my_image.jpg', 'r') >>> file_id = fs.put(my_image) // Read file >>> fs.get(file_id).read() GridFS Save files in mongoDB Stream data back to the client
  • 33. Building Your First MongoDB App • 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 :-)
  • 34. Requirements "As a user I want to be able to find other locations nearby" • Need to store locations (Offices, Restaurants etc) • name, address, tags • coordinates • user generated content e.g. tips / notes
  • 35. Requirements "As a user I want to be able to 'checkin' to a location" Checkins • User should be able to 'check in' to a location • Want to be able to generate statistics: • Recent checkins • Popular locations
  • 36. > location_1 = { name: "Holiday Inn", address: "Engelhardsgasse 12", city: "Nürnberg ", post_code: "D-90402" } Locations v1
  • 37. > location_1 = { name: "Holiday Inn", address: "Engelhardsgasse 12", city: "Nürnberg ", post_code: "D-90402" } > db.locations.save(location_1) > db.locations.find({name: "Holiday Inn"}) Locations v1
  • 38. > location_2 = { name: "Holiday Inn", address: "Engelhardsgasse 12", city: "Nürnberg ", post_code: "D-90402", tags: ["hotel", "conference", "mongodb"] } Locations v2
  • 39. > location_2 = { name: "Holiday Inn", address: "Engelhardsgasse 12", city: "Nürnberg ", post_code: "D-90402", tags: ["hotel", "conference", "mongodb"] } > db.locations.ensureIndex({tags: 1}) > db.locations.find({tags: "hotel"}) Locations v2
  • 40. > location_3 = { name: "Holiday Inn", address: "Engelhardsgasse 12", city: "Nürnberg ", post_code: "D-90402", tags: ["hotel", "conference", "mongodb"], lat_long: [52.5184, 13.387] } Locations v3
  • 41. > location_3 = { name: "Holiday Inn", address: "Engelhardsgasse 12", city: "Nürnberg ", post_code: "D-90402", tags: ["hotel", "conference", "mongodb"], lat_long: [52.5184, 13.387] } > db.locations.ensureIndex({lat_long: "2d"}) > db.locations.find({lat_long: {$near:[52.53, 13.4]}}) Locations v3
  • 42. // creating your indexes: > db.locations.ensureIndex({name: 1}) > db.locations.ensureIndex({tags: 1}) > db.locations.ensureIndex({lat_long: "2d"}) // with regular expressions: > db.locations.find({name: /^holid/i}) // by tag: > db.locations.find({tag: "hotel"}) // finding places: > db.locations.find({lat_long: {$near:[52.53, 13.4]}}) Finding locations
  • 43. // initial data load: > db.locations.insert(location_3) // adding a tip with update: > db.locations.update( {name: "Holiday Inn"}, {$push: { tips: { user: "Ross", date: ISODate("05-04-2012"), tip: "Check out my replication talk later!"} }}) Inserting locations - adding tips
  • 44. > db.locations.findOne() { _id : ObjectId("5c4ba5c0672c685e5e8aabf3"), name: "Holiday Inn", address: "Engelhardsgasse 12", city: "Nürnberg ", post_code: "D-90402", tags: ["hotel", "conference", "mongodb"], lat_long: [52.5184, 13.387], tips:[{ user: "Ross", date: ISODate("05-04-2012"), tip: "Check out my replication talk later!" }] } Tips added
  • 45. Requirements "As a user I want to be able to find other locations nearby" Need to store locations (Offices, Restaurants etc) • name, address, tags • coordinates • user generated content e.g. tips / notes "As a user I want to be able to 'checkin' to a location" Checkins • User should be able to 'check in' to a location • Want to be able to generate statistics: • Recent checkins • Popular locations
  • 46. > user_1 = { _id: "ross@10gen.com", name: "Ross", twitter: "RossC0", checkins: [ {location: "Holiday Inn", ts: "25/04/2012"}, {location: "Munich Airport", ts: "24/04/2012"} ] } Users and Checkins
  • 47. // find all users who've checked in here: > db.users.find({"checkins.location":"Holiday Inn"}) Simple Stats
  • 48. // find all users who've checked in here: > db.users.find({"checkins.location":"Holiday Inn"}) // Can't find the last 10 checkins easily > db.users.find({"checkins.location":"Holiday Inn"}) .sort({"checkins.ts": -1}).limit(10) Schema hard to query for stats. Simple Stats
  • 49. > user_2 = { _id: "ross@10gen.com", name: "Ross", twitter: "RossC0", } > checkin_1 = { location: location_id, user: user_id, ts: ISODate("05-04-2012") } > db.checkins.find({user: user_id}) User and Checkins v2
  • 50. // find all users who've checked in here: > loc = db.locations.findOne({"name":"Holiday Inn"}) > checkins = db.checkins.find({location: loc._id}) > u_ids = checkins.map(function(c){return c.user}) > users = db.users.find({_id: {$in: u_ids}}) // find the last 10 checkins here: > db.checkins.find({location: loc._id}) .sort({ts: -1}).limit(10) // count how many checked in today: > db.checkins.find({location: loc._id, ts: {$gt: midnight}} ).count() Simple Stats
  • 51. // Find most popular locations > map_func = function() { emit(this.location, 1); } > reduce_func = function(key, values) { return Array.sum(values); } > db.checkins.mapReduce(map_func, reduce_func, {query: {ts: {$gt: now_minus_3_hrs}}, out: "result"}) > db.result.findOne() {"_id": "Holiday Inn", "value" : 99} Map Reduce
  • 52. // Find most popular locations > agg = db.checkins.aggregate( {$match: {ts: {$gt: now_minus_3_hrs}}}, {$group: {_id: "$location", value: {$sum: 1}}} ) > agg.result [{"_id": "Holiday Inn", "value" : 17}] Aggregation
  • 53. P Deployment • Single server - need a strong backup plan
  • 54. Deployment • Single server - need a strong backup plan • Replica sets - High availability - Automatic failover P P S S
  • 55. Deployment • Single server - need a strong backup plan • Replica sets - High availability - Automatic failover • Sharded - Horizontally scale - Auto balancing P S S P S S P P S S
  • 56. MongoDB Use Cases • Archiving • Content Management • Ecommerce • Finance • Gaming • Government • Metadata Storage • News & Media • Online Advertising • Online Collaboration • Real-time stats/analytics • Social Networks • Telecommunications
  • 59. @mongodb conferences, appearances, and meetups http://www.10gen.com/events http://bit.ly/mongofb Facebook | Twitter | LinkedIn http://linkd.in/joinmongo download at mongodb.org support, training, and this talk brought to you by