+ All Categories
Home > Technology > Agile Schema Design: An introduction to MongoDB

Agile Schema Design: An introduction to MongoDB

Date post: 11-Nov-2014
Category:
Upload: stephen-steneker
View: 900 times
Download: 5 times
Share this document with a friend
Description:
Agile schema design with MongoDB talk from CampJS II (August, 2013). http://campjs.com/
Popular Tags:
58
Technical Services Engineer, 10gen Australia Stephen Steneker @ stennie #CampJS Agile Schema Design: An introduction to MongoDB
Transcript
Page 1: Agile Schema Design: An introduction to MongoDB

Technical Services Engineer, 10gen Australia

Stephen Steneker @stennie

#CampJS

Agile Schema Design: An introduction to MongoDB

Page 2: Agile Schema Design: An introduction to MongoDB

What is MongoDB?

Page 3: Agile Schema Design: An introduction to MongoDB

MongoDB is a ___________ database

•  Document

•  Open source

•  High performance

•  Horizontally scalable

•  Full featured

Page 4: Agile Schema Design: An introduction to MongoDB

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

Page 5: Agile Schema Design: An introduction to MongoDB

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

Page 6: Agile Schema Design: An introduction to MongoDB

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

Page 7: Agile Schema Design: An introduction to MongoDB

www.etiennemansard.com

Horizontally Scalable

Page 8: Agile Schema Design: An introduction to MongoDB

Full Featured

•  Ad Hoc queries

•  Real time aggregation

•  Rich query capabilities

•  Traditionally consistent

•  Geospatial features

•  Support for most programming languages

•  Flexible schema

Page 9: Agile Schema Design: An introduction to MongoDB

Database Landscape

Depth of Functionality

Scal

abili

ty &

Per

form

ance

Memcached

MongoDB

RDBMS

Page 10: Agile Schema Design: An introduction to MongoDB

Thousands  of  organisa.ons  are  using  MongoDB.  

Page 11: Agile Schema Design: An introduction to 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

Page 12: Agile Schema Design: An introduction to MongoDB

Document Database

Page 13: Agile Schema Design: An introduction to MongoDB

RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard

Terminology

Page 14: Agile Schema Design: An introduction to MongoDB

Typical (relational) ERD

User·Name·Email address

Category·Name·URL

Comment·Comment·Date·Author

Article·Name·Slug·Publish date·Text

Tag·Name·URL

Page 15: Agile Schema Design: An introduction to MongoDB

MongoDB ERD

User·Name·Email address

Article·Name·Slug·Publish date·Text·Author

Comment[]·Comment·Date·Author

Tag[]·Value

Category[]·Value

Page 16: Agile Schema Design: An introduction to MongoDB

http://www.flickr.com/photos/somegeekintn/3484353131/

We will build a library management application

Page 17: Agile Schema Design: An introduction to MongoDB

First step in any application is Determine your entities

Page 18: Agile Schema Design: An introduction to MongoDB

Library Management Application Entities •  Library Patrons (users)

•  Books (catalog)

•  Authors

•  Publishers

•  Categories ??

Page 19: Agile Schema Design: An introduction to MongoDB

In a relational based app We would start by doing schema design

Page 20: Agile Schema Design: An introduction to MongoDB

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

Page 21: Agile Schema Design: An introduction to MongoDB

In a MongoDB based app We start building our app and let the schema evolve

Page 22: Agile Schema Design: An introduction to MongoDB

MongoDB collections

•  Users

•  Books

•  Authors

•  Publishers

Page 23: Agile Schema Design: An introduction to MongoDB

Working with MongoDB

Page 24: Agile Schema Design: An introduction to MongoDB

Examples using mongo shell

Page 25: Agile Schema Design: An introduction to MongoDB

user = { username: 'stennie', first_name: 'Stephen', last_name: 'Steneker'

}

Start with an object (or array, hash, dict, etc)

Page 26: Agile Schema Design: An introduction to MongoDB

> use library switched to db library > db.users.insert(user)

Insert the record

No database or collection creation needed

Page 27: Agile Schema Design: An introduction to MongoDB

> db.users.findOne()

{

"_id" : ObjectId("50804d0bd94ccab2da652599"),

"username" : "stennie",

"first_name" : "Stephen",

"last_name" : "Steneker"

}

Querying for the user

Page 28: Agile Schema Design: An introduction to MongoDB

_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

Page 29: Agile Schema Design: An introduction to MongoDB

ObjectId

•  ObjectId is a special 12 byte value

•  Guaranteed to be unique across your cluster

•  ObjectId("50804d0bd94ccab2da652599") |-------------||---------||-----||----------| ts mac pid inc

Page 30: Agile Schema Design: An introduction to MongoDB

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

})

Creating an Author

Page 31: Agile Schema Design: An introduction to MongoDB

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

}

Querying for our Author

Page 32: Agile Schema Design: An introduction to MongoDB

> 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'),

}

})

Creating a Book

http://society6.com/PastaSoup/The-Fellowship-of-the-Ring-ZZc_Print/

Page 33: Agile Schema Design: An introduction to MongoDB

> db.books.findOne({language: 'english'}, {genre: 1})

{

"_id" : ObjectId("50804391d94ccab2da652598"),

"genre" : [

"fantasy",

"adventure"

]

}

Multiple values per key

Page 34: Agile Schema Design: An introduction to MongoDB

> db.books.findOne({genre: 'fantasy'}, {title: 1})

{

"_id" : ObjectId("50804391d94ccab2da652598"),

"title" : "fellowship of the ring, the"

}

Querying for key with multiple values

Query key with single value or multiple values the same way.

Page 35: Agile Schema Design: An introduction to MongoDB

> db.books.findOne({}, {publication: 1})

{

"_id" : ObjectId("50804ec7d94ccab2da65259a"),

"publication" : {

"name" : "george allen & unwin",

"location" : "London",

"date" : ISODate("1954-07-21T04:00:00Z")

}

}

Nested values

Page 36: Agile Schema Design: An introduction to MongoDB

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

}

Reach into nested values using dot notation

Page 37: Agile Schema Design: An introduction to MongoDB

> db.books.update(

{"_id" : ObjectId("50804391d94ccab2da652598")},

{ $set : {

isbn: '0547928211',

pages: 432

}

})

Update books

Change how you work with the data and the database follows

Page 38: Agile Schema Design: An introduction to MongoDB

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"

}

The updated Book document

Page 39: Agile Schema Design: An introduction to MongoDB

> db.books.ensureIndex({title: 1}) > db.books.ensureIndex({genre : 1}) > db.books.ensureIndex({'publication.date': -1})

Creating indexes

1: ascending sort -1: descending sort

Page 40: Agile Schema Design: An introduction to MongoDB

> 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"

}

Querying with regular expressions

Page 41: Agile Schema Design: An introduction to MongoDB

> 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'), }

})

Adding a few more books

http://society6.com/PastaSoup/The-Two-Towers-XTr_Print/

Page 42: Agile Schema Design: An introduction to MongoDB

> 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'), }

})

Adding a few more books

http://society6.com/PastaSoup/The-Return-of-the-King-Jsc_Print/

Page 43: Agile Schema Design: An introduction to MongoDB

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

}

Cursors

Page 44: Agile Schema Design: An introduction to MongoDB

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);

Paging

Page 45: Agile Schema Design: An introduction to MongoDB

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

Finding author by book

Page 46: Agile Schema Design: An introduction to MongoDB

MongoDB Drivers

Page 47: Agile Schema Design: An introduction to MongoDB

Real applications are not built in the shell

Page 48: Agile Schema Design: An introduction to MongoDB

MongoDB has native bindings for over 12 languages

Page 49: Agile Schema Design: An introduction to MongoDB
Page 50: Agile Schema Design: An introduction to MongoDB
Page 51: Agile Schema Design: An introduction to MongoDB

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)

Page 52: Agile Schema Design: An introduction to MongoDB

Next Steps

Page 53: Agile Schema Design: An introduction to MongoDB

Install from Homebrew, Mac Ports, tarball, or m!

Page 54: Agile Schema Design: An introduction to MongoDB

npm install -g m m latest

Page 55: Agile Schema Design: An introduction to MongoDB

http://education.10gen.com

Page 56: Agile Schema Design: An introduction to MongoDB

M101JS starts on Monday!

Page 57: Agile Schema Design: An introduction to MongoDB

http://mongodbsydney2013.eventbrite.com/

Page 58: Agile Schema Design: An introduction to MongoDB

Technical Services Engineer, 10gen Australia

Stephen Steneker @stennie

#CampJS

Questions?


Recommended