49

Dev Jumpstart: Build Your First App with MongoDB

  • Upload
    mongodb

  • View
    769

  • Download
    0

Embed Size (px)

DESCRIPTION

MongoDB SF 2014

Citation preview

Page 1: Dev Jumpstart: Build Your First App with MongoDB
Page 2: Dev Jumpstart: Build Your First App with MongoDB

Building Your First App With

MongoDB

Andrew Erlichson,

Vice President of Engineering

Developer Experience

Page 3: Dev Jumpstart: Build Your First App with MongoDB

What is MongoDB

Page 4: Dev Jumpstart: Build Your First App with 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: Dev Jumpstart: Build Your First App with MongoDB

Terminology

RDBMS MongoDB

Table, View ➜ Collection

Row ➜ Document

Index ➜ Index

Join ➜ Embedded Document

Foreign Key ➜ Reference

Partition ➜ Shard

Page 6: Dev Jumpstart: Build Your First App with MongoDB

Open Source

• MongoDB is an open source project

• On GitHub

• Licensed under the AGPL

• Started & sponsored by MongoDB, Inc.

• Commercial licenses available

• Contributions welcome

Page 7: Dev Jumpstart: Build Your First App with MongoDB

Horizontally Scalable

Page 8: Dev Jumpstart: Build Your First App with MongoDB

Database Landscape

Page 9: Dev Jumpstart: Build Your First App with MongoDB

Full Featured

• Ad Hoc queries

• Real time aggregation

• Rich query capabilities

• Strongly consistent

• Geospatial features

• Support for most programming languages

• Flexible schema

Page 10: Dev Jumpstart: Build Your First App with MongoDB

http://www.mongodb.org/downloads

Page 11: Dev Jumpstart: Build Your First App with MongoDB

$ tar xvf mongodb-osx-x86_64-2.6.5.tgz

$ cd mongodb-osx-x86_64-2.6.5/bin

$ mkdir –p /data/db

$ ./mongod

Running MongoDB

Page 12: Dev Jumpstart: Build Your First App with MongoDB

Andrews-Air:MongoDB-SF2014 aje$ mongo

MongoDB shell version: 2.6.5

connecting to: test

Server has startup warnings:

2014-12-01T23:29:15.317-0500 [initandlisten]

2014-12-01T23:29:15.317-0500 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

aje_air:PRIMARY> db.names.insert({'fullname':'Andrew Erlichson'});

WriteResult({ "nInserted" : 1 })

aje_air:PRIMARY> db.names.findOne();

{

"_id" : ObjectId("547dd9a0f907ebca54d1d994"),

"fullname" : "Andrew Erlichson"

}

aje_air:PRIMARY>

Mongo Shell

Page 13: Dev Jumpstart: Build Your First App with MongoDB

Web Demo

• MongoDB

• Python

• Bottle web framework

• Pymongo

Page 14: Dev Jumpstart: Build Your First App with MongoDB

from pymongo import MongoClientfrom bottle import route, run, template

@route('/')

def index():

collection = db.namesdoc = collection.find_one()return "Hello " + doc['fullname']

client = MongoClient('localhost', 27017)

db = client.test

run(host='localhost', port=8080)

hello.py

Page 15: Dev Jumpstart: Build Your First App with MongoDB

@route('/')

def index():

collection = db.namesdoc = collection.find_one()return "Hello " + doc['fullname']

client = MongoClient('localhost', 27017)

db = client.test

run(host='localhost', port=8080)

hello.py

Import the modules needed for Pymongo and the bottle web

framework

Page 16: Dev Jumpstart: Build Your First App with MongoDB

from pymongo import MongoClientfrom bottle import route, run, template

@route('/')

def index():

collection = db.namesdoc = collection.find_one()return "Hello " + doc['fullname']

run(host='localhost', port=8080)

hello.py

Connect to the MongoDBDatabase on Localhost and

use the “test” database

Page 17: Dev Jumpstart: Build Your First App with MongoDB

from pymongo import MongoClientfrom bottle import route, run, template

client = MongoClient('localhost', 27017)

db = client.test

run(host='localhost', port=8080)

hello.py

Define a handler that runs when user hits the root of our web

servers. That handler does a single query to the database and prints

back to the web browser

Page 18: Dev Jumpstart: Build Your First App with MongoDB

from pymongo import MongoClientfrom bottle import route, run, template

@route('/')

def index():

collection = db.namesdoc = collection.find_one()return "Hello " + doc['fullname']

client = MongoClient('localhost', 27017)

db = client.test

hello.py

Start the webserver on locahost, listening on

port 8080

Page 19: Dev Jumpstart: Build Your First App with MongoDB

Let’s Build a Blog

Page 20: Dev Jumpstart: Build Your First App with MongoDB

Determine Your Entities

First Step In Your App

Page 21: Dev Jumpstart: Build Your First App with MongoDB

Entities in our Blogging System

• Users (post authors)

• Posts

• Comments

• Tags

Page 22: Dev Jumpstart: Build Your First App with MongoDB

We Would Start By Doing Schema

Design

In a relational based app

Page 23: Dev Jumpstart: Build Your First App with MongoDB

Typical (relational) ERD

tag_idtag

tags

post_idpost_titlebodypost_datepost_author_uid

post_idcomment_idcommentauthor_namecomment_dateauthor_email

uidusernamepasswordEmail

post_idtag_id

users

posts comments

post_tags

Page 24: Dev Jumpstart: Build Your First App with MongoDB

We Start By Building Our App

And Let The Schema Evolve

Page 25: Dev Jumpstart: Build Your First App with MongoDB

MongoDB ERD

titlebodydateusername

Posts

[ ] comments

[ ] tags

Usernamepasswordemail

Users

Page 26: Dev Jumpstart: Build Your First App with MongoDB

Working in The Shell

Page 27: Dev Jumpstart: Build Your First App with MongoDB

user = {

_id: ’erlichson',

"password" : "a7cf1c46861b140894e1371a0eb6cd6791ca2e339f1a8d83a1846f6c81141dec,zYJue",

,

email: ’[email protected]',

}

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

Page 28: Dev Jumpstart: Build Your First App with MongoDB

> db.users.insert(user)

Insert the record

No collection creation needed

Page 29: Dev Jumpstart: Build Your First App with MongoDB

> db.users.findOne()

{

"_id" : "erlichson",

"password" : "a7cf1c46861b140894e1371a0eb6cd6791ca2e339f1a8d83a1846f6c81141dec,zYJue",

"email" : “[email protected]

}

Querying for the user

Page 30: Dev Jumpstart: Build Your First App with MongoDB

> db.posts.insert({

title: ‘Hello World’,

body: ‘This is my first blog post’,

date: new Date(‘2013-06-20’),

username: ‘erlichson’,

tags: [‘adventure’, ‘mongodb’],

comments: []

})

Creating a blog post

Page 31: Dev Jumpstart: Build Your First App with MongoDB

db.posts.find().pretty()

"_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"),

"title" : "Hello World",

"body" : "This is my first blog post",

"date" : ISODate("2013-06-20T00:00:00Z"),

"username" : "erlichson",

"tags" : [

"adventure",

"mongodb"

],

"comments" : [ ]

}

Finding the Post

Page 32: Dev Jumpstart: Build Your First App with MongoDB

> db.posts.find({tags:'adventure'}).pretty()

{

"_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"),

"title" : "Hello World",

"body" : "This is my first blog post",

"date" : ISODate("2013-06-20T00:00:00Z"),

"username" : "erlichson",

"tags" : [

"adventure",

"mongodb"

],

"comments" : [ ]

}

Querying an Array

Page 33: Dev Jumpstart: Build Your First App with MongoDB

> db.posts.update({_id:

new ObjectId("51c3bcddfbd5d7261b4cdb5b")},

{$push:{comments:

{name: 'Steve Blank', comment: 'Awesome Post'}}})

>

Using Update to Add a

Comment

Page 34: Dev Jumpstart: Build Your First App with MongoDB

> {_id:

new ObjectId("51c3bcddfbd5d7261b4cdb5b")},

>

Using Update to Add a

Comment

Predicate of the query. Specifies which document to update

Page 35: Dev Jumpstart: Build Your First App with MongoDB

> db.posts.update({_id:

new ObjectId("51c3bcddfbd5d7261b4cdb5b")},

{$push:{comments:

{name: 'Steve Blank', comment: 'Awesome Post'}}})

>

Using Update to Add a

Comment

“push” a new document under the “comments” array

Page 36: Dev Jumpstart: Build Your First App with MongoDB

> db.posts.findOne({_id: new ObjectId("51c3bcddfbd5d7261b4cdb5b")})

{

"_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"),

"body" : "This is my first blog post",

"comments" : [

{

"name" : "Steve Blank",

"comment" : "Awesome Post"

}

],

"date" : ISODate("2013-06-20T00:00:00Z"),

"tags" : [

"adventure",

"mongodb"

],

"title" : "Hello World",

"username" : "erlichson"

}

Post with Comment Attached

Page 37: Dev Jumpstart: Build Your First App with MongoDB

Completed Blog Demo

• Python

• Bottle.py

• Pymongo

• Features Supported

– Creating users

– Logging in

– Logging out

– Displaying Content

– Adding a new Blog Post

Page 38: Dev Jumpstart: Build Your First App with MongoDB

MongoDB Drivers

Page 39: Dev Jumpstart: Build Your First App with MongoDB
Page 40: Dev Jumpstart: Build Your First App with MongoDB
Page 41: Dev Jumpstart: Build Your First App with MongoDB

http://docs.mongodb.org/ecosystem/drivers/

Page 42: Dev Jumpstart: Build Your First App with MongoDB

Next Steps

Page 43: Dev Jumpstart: Build Your First App with MongoDB

http://docs.mongodb.org/manual/

Page 44: Dev Jumpstart: Build Your First App with MongoDB

MongoDB University

Page 45: Dev Jumpstart: Build Your First App with MongoDB

Data Modeling Deep Dive

2pm in Robertson Auditorium 1

Page 46: Dev Jumpstart: Build Your First App with MongoDB

Replication Internals

2pm in Fischer Banquet Room West

Page 47: Dev Jumpstart: Build Your First App with MongoDB

MongoDB Performance Debugging

11:40am in Robertson Auditorium 3

Page 48: Dev Jumpstart: Build Your First App with MongoDB

How to Achieve Scale

11:40am in Robertson Auditorium 2

Page 49: Dev Jumpstart: Build Your First App with MongoDB