building your first app: an introduction to mongodb

53
Building Your First App: An Introduction to MongoDB Robert Stam Software Engineer MongoDB, Inc. Great Wide Open Conference Atlanta, April 2, 2014

Upload: great-wide-open

Post on 26-Jan-2015

109 views

Category:

Technology


1 download

DESCRIPTION

Great Wide Open - Day 1 Robert Stam - MongoDB 2:30 PM - Operations 1 (Databases)

TRANSCRIPT

Page 1: Building Your First App: An Introduction to MongoDB

Building Your First App: An

Introduction to MongoDB

Robert StamSoftware Engineer

MongoDB, Inc.

Great Wide Open Conference

Atlanta, April 2, 2014

Page 2: Building Your First App: An Introduction to MongoDB

What is MongoDB?

Page 3: Building Your First App: An Introduction to MongoDB

3

• Document

• Open source

• High performance

• Horizontally scalable

• Full featured

MongoDB is a __________ database

Page 4: Building Your First App: An Introduction to MongoDB

4

• Not for .PDF & .DOC files

• A document is essentially an associative array

• Document == JSON object

• Document == PHP Array

• Document == Python Dict

• Document == Ruby Hash

• Document == C# BsonDocument object model

• etc

Document Database

Page 5: Building Your First App: An Introduction to MongoDB

5

• MongoDB is an open source project

• On GitHub

• Licensed under the AGPL

• Started & sponsored by MongoDB, Inc

• Commercial licenses available

• Contributions welcome

Open Source

Page 6: Building Your First App: An Introduction to MongoDB

Database Landscape

Page 7: Building Your First App: An Introduction to MongoDB

7

• Written in C++

• Runs nearly everywhere

• Extensive use of memory-mapped files

i.e. read-through write-through memory caching.

• Data serialized as BSON (fast parsing)

• Full support for primary & secondary indexes

• Document model = less work

High Performance

Page 8: Building Your First App: An Introduction to MongoDB

8

• Ad Hoc queries

• Rich query capabilities

• Geospatial features

• Real time aggregation

• Eventually consistent

• Support for many programming languages

• Flexible schema

Full Featured

Page 9: Building Your First App: An Introduction to MongoDB

9

Indexing @

Page 10: Building Your First App: An Introduction to MongoDB

10

Replication @

Page 11: Building Your First App: An Introduction to MongoDB

11

Sharding @

Page 12: Building Your First App: An Introduction to MongoDB

mongodb.org/downloads

Page 13: Building Your First App: An Introduction to MongoDB

Document Database

Page 14: Building Your First App: An Introduction to MongoDB

RDBMS MongoDB

Table, View Collection

Row Document

Index Index

Join Embedded Document

Foreign Key Reference

Partition Shard

Terminology

Page 15: Building Your First App: An Introduction to MongoDB

Typical (relational) ERD

Page 16: Building Your First App: An Introduction to MongoDB

MongoDB ERD

Page 17: Building Your First App: An Introduction to MongoDB

We will build a library management

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

Page 18: Building Your First App: An Introduction to MongoDB

First step in any application

Determine your entities

Page 19: Building Your First App: An Introduction to MongoDB

19

• Library Patrons (users)

• Books (card catalog)

• Authors

• Publishers

Library Management Application

Entities

Page 20: Building Your First App: An Introduction to MongoDB

In a relational based app

We would start by doing schema

design

Page 21: Building Your First App: An Introduction to MongoDB

21

• Users

• Books

• Authors

• Publishers

MongoDB Collections

Page 22: Building Your First App: An Introduction to MongoDB

Working with MongoDB

Page 23: Building Your First App: An Introduction to MongoDB

No common programming language

so we are using the MongoDB shell

Page 24: Building Your First App: An Introduction to MongoDB

24

> var user = {

username : "fred.jones",

first_name : "fred",

last_name : "jones"

}

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

Page 25: Building Your First App: An Introduction to MongoDB

25

> use library

> db.users.insert(user)

Insert the Record

No database or collection creation needed

Page 26: Building Your First App: An Introduction to MongoDB

26

> db.users.findOne()

{

_id : ObjectId("50804d0bd94ccab2da652599"),

username : "fred.jones",

first_name : "fred",

last_name : "jones"

}

Querying for the user

Page 27: Building Your First App: An Introduction to MongoDB

27

• _id is the primary key in MongoDB

• Automatically created as an ObjectId if not

provided

• Any unique immutable value could be used

• Automatically indexed

_id

Page 28: Building Your First App: An Introduction to MongoDB

28

• ObjectId is a special 12 byte value

• Guaranteed to be unique across your cluster

• ObjectId("50804d0bd94ccab2da652599")

ObjectId

Timestamp Machine Pid Increment

50804d0b d94cca b2da 652599

Page 29: Building Your First App: An Introduction to MongoDB

29

> 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 30: Building Your First App: An Introduction to MongoDB

30

> 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 : ISODate("1954-07-21T00:00:00Z")

}

})

Creating a Book

Page 31: Building Your First App: An Introduction to MongoDB

31

> db.books.findOne(

{ language : "english" },

{ genre : 1 })

{

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

"genre" : [

"fantasy",

"adventure"

]

}

Multiple Values per Key

Page 32: Building Your First App: An Introduction to MongoDB

32

> db.books.findOne(

{ genre : "fantasy" },

{ title : 1 })

{

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

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

}

Querying for key with

multiple values

Page 33: Building Your First App: An Introduction to MongoDB

33

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

{

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

"publication" : {

"name" : "george allen & unwin",

"location" : "London",

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

}

}

Nested Values

Page 34: Building Your First App: An Introduction to MongoDB

34

> db.books.update(

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

{ $set : {

isbn : "0547928211",

pages : 432

}}

)

True agile development. Simply Change how you work with the

data and the database follows

Update Books

Page 35: Building Your First App: An Introduction to MongoDB

35

> db.books.findOne()

{

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

"author" : ObjectId("507ffbb1d94ccab2da652597"),

"genre" : [ "fantasy", "adventure" ],

"isbn" : "0547928211",

"language" : "english",

"pages" : 432,

"publication" : {

"name" : "george allen & unwin",

"location" : "London",

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

},

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

}

Update Books

Page 36: Building Your First App: An Introduction to MongoDB

36

> db.books.ensureIndex({ title : 1 })

> db.books.ensureIndex({ genre : 1 }) // multikey

> db.books.ensureIndex({ "publication.date" : 1 })

Creating Indexes

Page 37: Building Your First App: An Introduction to MongoDB

37

> db.books.findOne({ title : /^fell/ })

{

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

"author" : ObjectId("507ffbb1d94ccab2da652597"),

"genre" : [ "fantasy", "adventure" ],

"isbn" : "0395082544",

"language" : "english",

"pages" : 432,

"publication" : {

"name" : "george allen & unwin",

"location" : "London",

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

},

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

}

Querying with RegEx

Page 38: Building Your First App: An Introduction to MongoDB

38

> db.books.insert({

title : "two towers, the",

author : ObjectID("507ffbb1d94ccab2da652597"),

language : "english",

isbn : "034523510X",

genre : [ "fantasy", "adventure" ],

page : 447,

publication : {

name : "george allen & unwin",

location : "London",

date : ISODate("1954-11-11T00:00:00Z")

}

})

Adding a Few More Books

Page 39: Building Your First App: An Introduction to MongoDB

39

> 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-20T00:00:00Z")

}

}

Cursors

Page 40: Building Your First App: An Introduction to MongoDB

40

> var page_num = 3;

> var results_per_page = 10;

> var cursor = db.books.find()

.sort({ publication_date : -1 })

.skip((page_num – 1) * results_per_page)

.limit(results_per_page);

Paging

Page 41: Building Your First App: An Introduction to MongoDB

41

> var 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 42: Building Your First App: An Introduction to MongoDB

MongoDB Drivers

Page 43: Building Your First App: An Introduction to MongoDB

Real Applications are not built in

the shell

Page 44: Building Your First App: An Introduction to MongoDB

MongoDB has native bindings

for over 12 languages

Page 45: Building Your First App: An Introduction to MongoDB
Page 46: Building Your First App: An Introduction to MongoDB
Page 47: Building Your First App: An Introduction to MongoDB

47

• Official Support for 12 languages

• Community drivers for many more

• Drivers connect applications 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, nuget)

MongoDB Drivers

Page 48: Building Your First App: An Introduction to MongoDB

48

docs.mongodb.org

Page 49: Building Your First App: An Introduction to MongoDB

49

Online Training at MongoDB University

Page 50: Building Your First App: An Introduction to MongoDB

Questions?

Page 51: Building Your First App: An Introduction to MongoDB

MongoDB WorldNew York City, June 23-25

Save 25% with Drivers25

Register at world.mongodb.com

See how Cisco, Stripe, Carfax, Expedia and

others are engineering the next generation of

data with MongoDB

Page 52: Building Your First App: An Introduction to MongoDB

MongoDB WorldNew York City, June 23-25

See what’s next in MongoDB including

• MongoDB 2.6

• Sharding

• Replication

• Aggregation

Save 25% with Drivers25

world.mongodb.com

Page 53: Building Your First App: An Introduction to MongoDB

Software Engineer, MongoDB Inc.

Robert Stam

Thank You