SlideShare a Scribd company logo
1 of 58
Download to read offline
#CampJS

Agile Schema Design:
An introduction to MongoDB
Stephen Steneker @stennie
Technical Services Engineer, 10gen Australia
What is MongoDB?
MongoDB is a ___________ database
•  Document
•  Open source
•  High performance
•  Horizontally scalable
•  Full featured
Document Database
•  Not for .PDF & .DOC files
•  A document is essentially an associative array
•  Document == JSON object
•  Document == PHP Array
•  Document == Python Dict
•  Document == Ruby Hash
•  etc
Open Source
•  MongoDB is an open source project
•  On GitHub (mongodb/mongo)
•  Licensed under the AGPL
•  Started & sponsored by 10gen
•  Commercial licenses available
•  Contributions welcome
High Performance
•  Written in C++
•  Extensive use of memory-mapped files

i.e. read-through write-through memory caching.
•  Runs nearly everywhere
•  Data serialized as BSON (fast parsing)
•  Full support for primary & secondary indexes
•  Document model = less work
www.etiennemansard.com

Horizontally Scalable
Full Featured
•  Ad Hoc queries
•  Real time aggregation
•  Rich query capabilities
•  Traditionally consistent
•  Geospatial features
•  Support for most programming languages
•  Flexible schema
Scalability & Performance

Memcached
MongoDB

RDBMS

Depth of Functionality

Database Landscape
Thousands	
  of	
  organisa.ons	
  are	
  
using	
  MongoDB.	
  
Common Use Cases
•  Content Management –MTV Networks, Forbes, Shutterfly
•  High Volume Data Feeds – ShareThis, Stripe, Wordnik
•  Operational Intelligence – Intuit, Buddy Media, Traackr
•  Product Data Management– CustomInk, Totsy
•  Social Networking – foursquare, Disney
More case studies and presentations:
http://www.10gen.com/use-cases
Document Database
RDBMS
Table, View
Row
Index
Join
Foreign Key
Partition

Terminology

➜
➜
➜
➜
➜
➜

MongoDB
Collection
Document
Index
Embedded Document
Reference
Shard
Category
·Name
·URL

User
·Name
·Email address

Article
·Name
·Slug
·Publish date
·Text

Tag
·Name
·URL

Comment
·Comment
·Date
·Author

Typical (relational) ERD
Article

User
·Name
·Email address

·Name
·Slug
·Publish date
·Text
·Author

Comment[]
·Comment
·Date
·Author

Tag[]
·Value

Category[]
·Value

MongoDB ERD
We will build a library
management application
http://www.flickr.com/photos/somegeekintn/3484353131/
First step in any application is

Determine your entities
Library Management Application
Entities
•  Library Patrons (users)
•  Books (catalog)
•  Authors
•  Publishers
•  Categories ??
In a relational based app

We would start by doing
schema design
Relational schema design
•  Large ERD Diagrams
•  Complex create table statements
•  ORMs to map tables to objects
•  Tables just to join tables together
•  For this simple app we'd have 5 tables and 5 join

tables
•  Lots of revisions until we get it just right
In a MongoDB based app

We start building our app
and let the schema evolve
MongoDB collections
•  Users
•  Books
•  Authors
•  Publishers
Working with MongoDB
Examples using mongo shell
Start with an object
(or array, hash, dict, etc)
user = {
username:

'stennie',

first_name: 'Stephen',
last_name:
}

'Steneker'
Insert the record
> use library
switched to db library
> db.users.insert(user)

No database or collection creation needed
Querying for the user
> db.users.findOne()
{
"_id" : ObjectId("50804d0bd94ccab2da652599"),
"username" : "stennie",
"first_name" : "Stephen",
"last_name" : "Steneker"
}
_id
•  _id is the primary key in MongoDB
•  Automatically indexed
•  Automatically created as an ObjectId if not

provided

•  Any unique immutable value could be used
ObjectId
•  ObjectId is a special 12 byte value
•  Guaranteed to be unique across your cluster
•  ObjectId("50804d0bd94ccab2da652599")

|-------------||---------||-----||---------|
ts
mac pid inc
Creating an Author
> db.author.insert({
first_name: 'j.r.r.',
last_name:

'tolkien',

bio: 'J.R.R. Tolkien (1892.1973), beloved
throughout the world as the creator of The Hobbit and
The Lord of the Rings, was a professor of Anglo-Saxon
at Oxford, a fellow of Pembroke College, and a fellow
of Merton College until his retirement in 1959. His
chief interest was the linguistic aspects of the early
English written tradition, but even as he studied these
classics he was creating a set of his own.'
})
Querying for our Author
> db.author.findOne( { last_name : 'tolkien' } )
{
"_id" : ObjectId("507ffbb1d94ccab2da652597"),
"first_name" : "j.r.r.",
"last_name" : "tolkien",
"bio" : "J.R.R. Tolkien (1892.1973), beloved
throughout the world as the creator of The Hobbit and
The Lord of the Rings, was a professor of Anglo-Saxon
at Oxford, a fellow of Pembroke College, and a fellow
of Merton College until his retirement in 1959. His
chief interest was the linguistic aspects of the early
English written tradition, but even as he studied these
classics he was creating a set of his own."
}
Creating a Book
> db.books.insert({
title: 'fellowship of the ring, the',
author: ObjectId("507ffbb1d94ccab2da652597"),
language: 'english',
genre: ['fantasy', 'adventure'],
publication: {
name: 'george allen & unwin',
location: 'London',
date: new Date('21 July 1954'),
}
})
http://society6.com/PastaSoup/The-Fellowship-of-the-Ring-ZZc_Print/
Multiple values per key
> db.books.findOne({language: 'english'}, {genre: 1})
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"genre" : [
"fantasy",
"adventure"
]
}
Querying for key with
multiple values
> db.books.findOne({genre: 'fantasy'}, {title: 1})
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"title" : "fellowship of the ring, the"
}

Query key with single value or
multiple values the same way.
Nested values
> db.books.findOne({}, {publication: 1})
{
"_id" : ObjectId("50804ec7d94ccab2da65259a"),
"publication" : {
"name" : "george allen & unwin",
"location" : "London",
"date" : ISODate("1954-07-21T04:00:00Z")
}
}
Reach into nested values
using dot notation
> db.books.findOne(
{'publication.date' :
{ $lt : new Date('21 June 1960')}
}
)
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"title" : "fellowship of the ring, the",
"author" : ObjectId("507ffbb1d94ccab2da652597"),
"language" : "english",
"genre" : [ "fantasy”, "adventure" ],
"publication" : {
"name" : "george allen & unwin",
"location" : "London",
"date" : ISODate("1954-07-21T04:00:00Z")
}
}
Update books
> db.books.update(
{"_id" : ObjectId("50804391d94ccab2da652598")},
{ $set : {
isbn: '0547928211',
pages: 432
}
})

Change how you work with
the data and the database follows
The updated Book document
db.books.findOne()
{
"_id" : ObjectId("50804ec7d94ccab2da65259a"),
"author" : ObjectId("507ffbb1d94ccab2da652597"),
"genre" : [ "fantasy", "adventure" ],
"isbn" : "0395082544",
"language" : "english",
"pages" : 432,
"publication" : {
"name" : "george allen & unwin",
"location" : "London",
"date" : ISODate("1954-07-21T04:00:00Z")
},
"title" : "fellowship of the ring, the"
}
Creating indexes
> db.books.ensureIndex({title: 1})
> db.books.ensureIndex({genre : 1})
>
db.books.ensureIndex({'publication.date':
-1})

1: ascending sort
-1: descending sort
Querying with regular expressions
> db.books.findOne({title : /^fell/})
{
"_id" : ObjectId("50804ec7d94ccab2da65259a"),
"author" : ObjectId("507ffbb1d94ccab2da652597"),
"genre" : [ "fantasy", "adventure" ],
"isbn" : "0395082544",
"language" : "english",
"pages" : 432,
"publication" : {
"name" : "george allen & unwin",
"location" : "London",
"date" : ISODate("1954-07-21T04:00:00Z")
},
"title" : "fellowship of the ring, the"
}
Adding a few more books
> db.books.insert({
title: 'two towers, the',
author: ObjectId("507ffbb1d94ccab2da652597"),
language: 'english',
isbn : "034523510X",
genre: ['fantasy', 'adventure'],
pages: 447,
publication: {
name: 'george allen & unwin',
location: 'London',
date: new Date('11 Nov 1954'),
}
})
http://society6.com/PastaSoup/The-Two-Towers-XTr_Print/
Adding a few more books
> db.books.insert({
title: 'return of the king, the',
author: ObjectId("507ffbb1d94ccab2da652597"),
language: 'english',
isbn : "0345248295",
genre: ['fantasy', 'adventure'],
pages: 544,
publication: {
name: 'george allen & unwin',
location: 'London',
date: new Date('20 Oct 1955'),
}
})
http://society6.com/PastaSoup/The-Return-of-the-King-Jsc_Print/
Cursors
> db.books.find(
{ author: ObjectId("507ffbb1d94ccab2da652597")})
.sort({ 'publication.date' : -1})
.limit(1)
{
"_id" : ObjectId("5080d33ed94ccab2da65259d"),
"title" : "return of the king, the",
"author" : ObjectId("507ffbb1d94ccab2da652597"),
"language" : "english",
"isbn" : "0345248295",
"genre" : [ "fantasy", "adventure" ],
"pages" : 544,
"publication" : {
"name" : "george allen & unwin",
"location" : "London",
"date" : ISODate("1955-10-20T04:00:00Z")
}
}
Paging
page_num = 3;
results_per_page = 10;
cursor = db.books.find()
.sort({ "publication.date" : -1 })
.skip((page_num - 1) * results_per_page)
.limit(results_per_page);
Finding author by book
Ø  book = db.books.findOne({"title" : "return of the

king, the”})
> db.author.findOne({_id: book.author})
{
"_id" : ObjectId("507ffbb1d94ccab2da652597"),
"first_name" : "j.r.r.",
"last_name" : "tolkien",
"bio" : "J.R.R. Tolkien (1892.1973), beloved
throughout the world as the creator of The Hobbit and
The Lord of the Rings, was a professor of Anglo-Saxon
at Oxford, a fellow of Pembroke College, and a fellow
of Merton College until his retirement in 1959. His
chief interest was the linguistic aspects of the early
English written tradition, but even as he studied these
classics he was creating a set of his own."
}
MongoDB Drivers
Real applications are not
built in the shell
MongoDB has native
bindings for over 12
languages
MongoDB drivers
•  Official Support for 12 languages
•  Community drivers for tons more
•  Drivers connect to mongo servers
•  Drivers translate BSON into native types
•  mongo shell is not a driver, but works like one in

some ways
•  Installed using typical means (npm, pecl, gem, pip)
Next Steps
Install from Homebrew, Mac Ports,
tarball, or m!
npm install -g m
m latest
http://education.10gen.com
M101JS starts on Monday!
http://mongodbsydney2013.eventbrite.com/
#CampJS

Questions?
Stephen Steneker @stennie
Technical Services Engineer, 10gen Australia

More Related Content

What's hot

Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDBrogerbodamer
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMike Friedman
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema DesignMongoDB
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema DesignMongoDB
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata CatalogBuilding Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Cataloghungarianhc
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema DesignAlex Litvinok
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDBMongoDB
 
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBLearn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBMarakana Inc.
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsMongoDB
 
Building a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and JavaBuilding a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and Javaantoinegirbal
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in DocumentsMongoDB
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real WorldMike Friedman
 
Webinar: Data Modeling Examples in the Real World
Webinar: Data Modeling Examples in the Real WorldWebinar: Data Modeling Examples in the Real World
Webinar: Data Modeling Examples in the Real WorldMongoDB
 
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 documentosConceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosMongoDB
 
Building Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBBuilding Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBMongoDB
 
Back to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsBack to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsJoe Drumgoole
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsBack to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsMongoDB
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationMongoDB
 

What's hot (19)

Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDB
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata CatalogBuilding Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Catalog
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBLearn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDB
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in Documents
 
Building a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and JavaBuilding a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and Java
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real World
 
Webinar: Data Modeling Examples in the Real World
Webinar: Data Modeling Examples in the Real WorldWebinar: Data Modeling Examples in the Real World
Webinar: Data Modeling Examples in the Real World
 
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 documentosConceptos 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
 
Building Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBBuilding Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDB
 
Back to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsBack to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in Documents
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsBack to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documents
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
 

Similar to Agile Schema Design: An introduction to MongoDB

buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdfbuildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdflallababa
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopSteven Francia
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDBMongoDB
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDBJeremy Taylor
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDBMike Friedman
 
Building Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBBuilding Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBMongoDB
 
Building Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBBuilding Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBGreat Wide Open
 
MongoDB Mojo: Building a Basic Perl App
MongoDB Mojo: Building a Basic Perl AppMongoDB Mojo: Building a Basic Perl App
MongoDB Mojo: Building a Basic Perl AppStennie Steneker
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data ModelingDATAVERSITY
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
C# Development (Sam Corder)
C# Development (Sam Corder)C# Development (Sam Corder)
C# Development (Sam Corder)MongoSF
 
Building Your First MongoDB App
Building Your First MongoDB AppBuilding Your First MongoDB App
Building Your First MongoDB AppHenrik Ingo
 
Schema design
Schema designSchema design
Schema designchristkv
 
Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDBMongoDB
 
Webinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB AppWebinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB AppMongoDB
 
Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014
Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014
Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014Alex Humphreys
 
Schema design mongo_boston
Schema design mongo_bostonSchema design mongo_boston
Schema design mongo_bostonMongoDB
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Schema & Design
Schema & DesignSchema & Design
Schema & DesignMongoDB
 

Similar to Agile Schema Design: An introduction to MongoDB (20)

buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdfbuildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
Building Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBBuilding Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDB
 
Building Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDBBuilding Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDB
 
MongoDB Mojo: Building a Basic Perl App
MongoDB Mojo: Building a Basic Perl AppMongoDB Mojo: Building a Basic Perl App
MongoDB Mojo: Building a Basic Perl App
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
 
Schema Design
Schema DesignSchema Design
Schema Design
 
C# Development (Sam Corder)
C# Development (Sam Corder)C# Development (Sam Corder)
C# Development (Sam Corder)
 
Building Your First MongoDB App
Building Your First MongoDB AppBuilding Your First MongoDB App
Building Your First MongoDB App
 
Schema design
Schema designSchema design
Schema design
 
Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDB
 
Webinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB AppWebinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB App
 
MongoDB at GUL
MongoDB at GULMongoDB at GUL
MongoDB at GUL
 
Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014
Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014
Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014
 
Schema design mongo_boston
Schema design mongo_bostonSchema design mongo_boston
Schema design mongo_boston
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema & Design
Schema & DesignSchema & Design
Schema & Design
 

Recently uploaded

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Agile Schema Design: An introduction to MongoDB

  • 1. #CampJS Agile Schema Design: An introduction to MongoDB Stephen Steneker @stennie Technical Services Engineer, 10gen Australia
  • 3. MongoDB is a ___________ database •  Document •  Open source •  High performance •  Horizontally scalable •  Full featured
  • 4. Document Database •  Not for .PDF & .DOC files •  A document is essentially an associative array •  Document == JSON object •  Document == PHP Array •  Document == Python Dict •  Document == Ruby Hash •  etc
  • 5. Open Source •  MongoDB is an open source project •  On GitHub (mongodb/mongo) •  Licensed under the AGPL •  Started & sponsored by 10gen •  Commercial licenses available •  Contributions welcome
  • 6. High Performance •  Written in C++ •  Extensive use of memory-mapped files i.e. read-through write-through memory caching. •  Runs nearly everywhere •  Data serialized as BSON (fast parsing) •  Full support for primary & secondary indexes •  Document model = less work
  • 8. Full Featured •  Ad Hoc queries •  Real time aggregation •  Rich query capabilities •  Traditionally consistent •  Geospatial features •  Support for most programming languages •  Flexible schema
  • 9. Scalability & Performance Memcached MongoDB RDBMS Depth of Functionality Database Landscape
  • 10. Thousands  of  organisa.ons  are   using  MongoDB.  
  • 11. Common Use Cases •  Content Management –MTV Networks, Forbes, Shutterfly •  High Volume Data Feeds – ShareThis, Stripe, Wordnik •  Operational Intelligence – Intuit, Buddy Media, Traackr •  Product Data Management– CustomInk, Totsy •  Social Networking – foursquare, Disney More case studies and presentations: http://www.10gen.com/use-cases
  • 16. We will build a library management application http://www.flickr.com/photos/somegeekintn/3484353131/
  • 17. First step in any application is Determine your entities
  • 18. Library Management Application Entities •  Library Patrons (users) •  Books (catalog) •  Authors •  Publishers •  Categories ??
  • 19. In a relational based app We would start by doing schema design
  • 20. Relational schema design •  Large ERD Diagrams •  Complex create table statements •  ORMs to map tables to objects •  Tables just to join tables together •  For this simple app we'd have 5 tables and 5 join tables •  Lots of revisions until we get it just right
  • 21. In a MongoDB based app We start building our app and let the schema evolve
  • 22. MongoDB collections •  Users •  Books •  Authors •  Publishers
  • 25. Start with an object (or array, hash, dict, etc) user = { username: 'stennie', first_name: 'Stephen', last_name: } 'Steneker'
  • 26. Insert the record > use library switched to db library > db.users.insert(user) No database or collection creation needed
  • 27. Querying for the user > db.users.findOne() { "_id" : ObjectId("50804d0bd94ccab2da652599"), "username" : "stennie", "first_name" : "Stephen", "last_name" : "Steneker" }
  • 28. _id •  _id is the primary key in MongoDB •  Automatically indexed •  Automatically created as an ObjectId if not provided •  Any unique immutable value could be used
  • 29. ObjectId •  ObjectId is a special 12 byte value •  Guaranteed to be unique across your cluster •  ObjectId("50804d0bd94ccab2da652599") |-------------||---------||-----||---------| ts mac pid inc
  • 30. Creating an Author > db.author.insert({ first_name: 'j.r.r.', last_name: 'tolkien', bio: 'J.R.R. Tolkien (1892.1973), beloved throughout the world as the creator of The Hobbit and The Lord of the Rings, was a professor of Anglo-Saxon at Oxford, a fellow of Pembroke College, and a fellow of Merton College until his retirement in 1959. His chief interest was the linguistic aspects of the early English written tradition, but even as he studied these classics he was creating a set of his own.' })
  • 31. Querying for our Author > db.author.findOne( { last_name : 'tolkien' } ) { "_id" : ObjectId("507ffbb1d94ccab2da652597"), "first_name" : "j.r.r.", "last_name" : "tolkien", "bio" : "J.R.R. Tolkien (1892.1973), beloved throughout the world as the creator of The Hobbit and The Lord of the Rings, was a professor of Anglo-Saxon at Oxford, a fellow of Pembroke College, and a fellow of Merton College until his retirement in 1959. His chief interest was the linguistic aspects of the early English written tradition, but even as he studied these classics he was creating a set of his own." }
  • 32. Creating a Book > db.books.insert({ title: 'fellowship of the ring, the', author: ObjectId("507ffbb1d94ccab2da652597"), language: 'english', genre: ['fantasy', 'adventure'], publication: { name: 'george allen & unwin', location: 'London', date: new Date('21 July 1954'), } }) http://society6.com/PastaSoup/The-Fellowship-of-the-Ring-ZZc_Print/
  • 33. Multiple values per key > db.books.findOne({language: 'english'}, {genre: 1}) { "_id" : ObjectId("50804391d94ccab2da652598"), "genre" : [ "fantasy", "adventure" ] }
  • 34. Querying for key with multiple values > db.books.findOne({genre: 'fantasy'}, {title: 1}) { "_id" : ObjectId("50804391d94ccab2da652598"), "title" : "fellowship of the ring, the" } Query key with single value or multiple values the same way.
  • 35. Nested values > db.books.findOne({}, {publication: 1}) { "_id" : ObjectId("50804ec7d94ccab2da65259a"), "publication" : { "name" : "george allen & unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") } }
  • 36. Reach into nested values using dot notation > db.books.findOne( {'publication.date' : { $lt : new Date('21 June 1960')} } ) { "_id" : ObjectId("50804391d94ccab2da652598"), "title" : "fellowship of the ring, the", "author" : ObjectId("507ffbb1d94ccab2da652597"), "language" : "english", "genre" : [ "fantasy”, "adventure" ], "publication" : { "name" : "george allen & unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") } }
  • 37. Update books > db.books.update( {"_id" : ObjectId("50804391d94ccab2da652598")}, { $set : { isbn: '0547928211', pages: 432 } }) Change how you work with the data and the database follows
  • 38. The updated Book document db.books.findOne() { "_id" : ObjectId("50804ec7d94ccab2da65259a"), "author" : ObjectId("507ffbb1d94ccab2da652597"), "genre" : [ "fantasy", "adventure" ], "isbn" : "0395082544", "language" : "english", "pages" : 432, "publication" : { "name" : "george allen & unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") }, "title" : "fellowship of the ring, the" }
  • 39. Creating indexes > db.books.ensureIndex({title: 1}) > db.books.ensureIndex({genre : 1}) > db.books.ensureIndex({'publication.date': -1}) 1: ascending sort -1: descending sort
  • 40. Querying with regular expressions > db.books.findOne({title : /^fell/}) { "_id" : ObjectId("50804ec7d94ccab2da65259a"), "author" : ObjectId("507ffbb1d94ccab2da652597"), "genre" : [ "fantasy", "adventure" ], "isbn" : "0395082544", "language" : "english", "pages" : 432, "publication" : { "name" : "george allen & unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") }, "title" : "fellowship of the ring, the" }
  • 41. Adding a few more books > db.books.insert({ title: 'two towers, the', author: ObjectId("507ffbb1d94ccab2da652597"), language: 'english', isbn : "034523510X", genre: ['fantasy', 'adventure'], pages: 447, publication: { name: 'george allen & unwin', location: 'London', date: new Date('11 Nov 1954'), } }) http://society6.com/PastaSoup/The-Two-Towers-XTr_Print/
  • 42. Adding a few more books > db.books.insert({ title: 'return of the king, the', author: ObjectId("507ffbb1d94ccab2da652597"), language: 'english', isbn : "0345248295", genre: ['fantasy', 'adventure'], pages: 544, publication: { name: 'george allen & unwin', location: 'London', date: new Date('20 Oct 1955'), } }) http://society6.com/PastaSoup/The-Return-of-the-King-Jsc_Print/
  • 43. Cursors > db.books.find( { author: ObjectId("507ffbb1d94ccab2da652597")}) .sort({ 'publication.date' : -1}) .limit(1) { "_id" : ObjectId("5080d33ed94ccab2da65259d"), "title" : "return of the king, the", "author" : ObjectId("507ffbb1d94ccab2da652597"), "language" : "english", "isbn" : "0345248295", "genre" : [ "fantasy", "adventure" ], "pages" : 544, "publication" : { "name" : "george allen & unwin", "location" : "London", "date" : ISODate("1955-10-20T04:00:00Z") } }
  • 44. Paging page_num = 3; results_per_page = 10; cursor = db.books.find() .sort({ "publication.date" : -1 }) .skip((page_num - 1) * results_per_page) .limit(results_per_page);
  • 45. Finding author by book Ø  book = db.books.findOne({"title" : "return of the king, the”}) > db.author.findOne({_id: book.author}) { "_id" : ObjectId("507ffbb1d94ccab2da652597"), "first_name" : "j.r.r.", "last_name" : "tolkien", "bio" : "J.R.R. Tolkien (1892.1973), beloved throughout the world as the creator of The Hobbit and The Lord of the Rings, was a professor of Anglo-Saxon at Oxford, a fellow of Pembroke College, and a fellow of Merton College until his retirement in 1959. His chief interest was the linguistic aspects of the early English written tradition, but even as he studied these classics he was creating a set of his own." }
  • 47. Real applications are not built in the shell
  • 48. MongoDB has native bindings for over 12 languages
  • 49.
  • 50.
  • 51. MongoDB drivers •  Official Support for 12 languages •  Community drivers for tons more •  Drivers connect to mongo servers •  Drivers translate BSON into native types •  mongo shell is not a driver, but works like one in some ways •  Installed using typical means (npm, pecl, gem, pip)
  • 53. Install from Homebrew, Mac Ports, tarball, or m!
  • 54. npm install -g m m latest
  • 56. M101JS starts on Monday!
  • 58. #CampJS Questions? Stephen Steneker @stennie Technical Services Engineer, 10gen Australia