Slide 1

Slide 1 text

Software Engineer, 10gen @brandonmblack Brandon Black #MongoDBDays Building Your First App: An Introduction to MongoDB

Slide 2

Slide 2 text

What is MongoDB?

Slide 3

Slide 3 text

MongoDB is a ___________ database •  Document •  Open source •  High performance •  Horizontally scalable •  Full featured

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Open Source •  MongoDB is an open source project •  On GitHub •  Licensed under the AGPL •  Started & sponsored by 10gen •  Commercial licenses available •  Contributions welcome

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

www.etiennemansard.com Horizontally Scalable

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Database Landscape Depth of Functionality Scalability & Performance Memcached MongoDB RDBMS

Slide 10

Slide 10 text

http://www.mongodb.org/downloads

Slide 11

Slide 11 text

Mongo Shell

Slide 12

Slide 12 text

Document Database

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Typical (relational) ERD User ·Name ·Email address Category ·Name ·URL Comment ·Comment ·Date ·Author Article ·Name ·Slug ·Publish date ·Text Tag ·Name ·URL

Slide 15

Slide 15 text

MongoDB ERD User ·Name ·Email address Article ·Name ·Slug ·Publish date ·Text ·Author Comment[] ·Comment ·Date ·Author Tag[] ·Value Category[] ·Value

Slide 16

Slide 16 text

http://www.flickr.com/photos/somegeekintn/3484353131/ Library Management Application

Slide 17

Slide 17 text

First step in any application is Determine your entities

Slide 18

Slide 18 text

Library Management Application Entities •  Library Patrons (users) •  Books (catalog) •  Authors •  Publishers •  Categories ??

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

MongoDB Collections •  Users •  Books •  Authors •  Publishers

Slide 23

Slide 23 text

No Common Language Mongo Shell

Slide 24

Slide 24 text

Working with MongoDB

Slide 25

Slide 25 text

user = { username: 'fred.jones', first_name: 'fred', last_name: 'jones', } Start with an Object (or array, hash, dict, etc)

Slide 26

Slide 26 text

> db.users.insert(user) Insert the Record No collection creation needed

Slide 27

Slide 27 text

> db.users.findOne() { "_id" : ObjectId("50804d0bd94ccab2da652599"), "username" : "fred.jones", "first_name" : "fred", "last_name" : "jones" } Querying for the User

Slide 28

Slide 28 text

_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

Slide 29

Slide 29 text

ObjectId •  ObjectId is a special 12 byte value •  Guaranteed to be unique across your cluster •  ObjectId("50804d0bd94ccab2da652599") |-------------||---------||-----||----------| ts mac pid inc

Slide 30

Slide 30 text

> 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

Slide 31

Slide 31 text

> 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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

> db.books.findOne({language: 'english'}, {genre: 1}) { "_id" : ObjectId("50804391d94ccab2da652598"), "genre" : [ "fantasy", "adventure" ] } Multiple Values Per Key

Slide 34

Slide 34 text

> db.books.findOne({genre: 'fantasy'}, {title: 1}) { "_id" : ObjectId("50804391d94ccab2da652598"), "title" : "fellowship of the ring, the" } Multiple Values Per Key Query key with single value or multiple values the same way.

Slide 35

Slide 35 text

> db.books.findOne({}, {publication: 1}) { "_id" : ObjectId("50804ec7d94ccab2da65259a"), "publication" : { "name" : "george allen & unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") } } Nested Values

Slide 36

Slide 36 text

> 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") } } Query Nested Values with Dot Notation

Slide 37

Slide 37 text

> db.books.update( {"_id" : ObjectId("50804391d94ccab2da652598")}, { $set : { isbn: '0547928211', pages: 432 } }) Update Books This is true agile development . I’m simply changing how I work with the data, and the database follows.

Slide 38

Slide 38 text

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" } Updated Book Record

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

> 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" } Query with Regular Expressions

Slide 41

Slide 41 text

> 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 More Books http://society6.com/PastaSoup/The-Two-Towers-XTr_Print/

Slide 42

Slide 42 text

> 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 More Books http://society6.com/PastaSoup/The-Return-of-the-King-Jsc_Print/

Slide 43

Slide 43 text

> 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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

> 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 an Author by Book

Slide 46

Slide 46 text

MongoDB Drivers

Slide 47

Slide 47 text

Real applications are not built in the shell

Slide 48

Slide 48 text

MongoDB Drivers •  Drivers connect to mongo servers •  Drivers translate BSON into native types •  The MongoDB shell is not a driver, but works like one in some ways •  Installed using typical means (npm, pecl, gem, pip)

Slide 49

Slide 49 text

MongoDB has native bindings for over 12 languages

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Next Steps

Slide 53

Slide 53 text

We've introduced a lot of concepts here

Slide 54

Slide 54 text

Schema Design @ 10:35am User ·Name ·Email address Article ·Name ·Slug ·Publish date ·Text ·Author Comment[] ·Comment ·Date ·Author Tag[] ·Value Category[] ·Value

Slide 55

Slide 55 text

Secondary Secondary Primary Client Application Driver Write Read Read Replication @ 12:15pm

Slide 56

Slide 56 text

7 16 1 2 5 6 9 12 18 21 B-Trees 13 {x: 13} Indexing @ 1:45pm

Slide 57

Slide 57 text

Node 1 Secondary Config Server Node 1 Secondary Config Server Node 1 Secondary Config Server Shard Shard Shard Mongos App Server Mongos App Server Mongos App Server Sharding @ 2:30pm

Slide 58

Slide 58 text

•  What’s next? –  Schema Design @ 10:35am –  Replication @ 12:15pm –  Indexing @ 1:45pm –  Sharding @ 2:30pm –  Webinar: Technical Overview of MongoDB (March 7th) –  MongoDB User Group •  Resources https://education.10gen.com/ http://www.10gen.com/presentations http://github.com/brandonblack/presentations

Slide 59

Slide 59 text

Software Engineer, 10gen @brandonmblack Brandon Black #MongoDBDays Thank You