Building Your First App with Shawn Mcarthy

42

Transcript of Building Your First App with Shawn Mcarthy

Page 1: Building Your First App with Shawn Mcarthy
Page 2: Building Your First App with Shawn Mcarthy

Building Your First App With MongoDB

Shawn McCarthy

Solutions Architect

Page 3: Building Your First App with Shawn Mcarthy

What is MongoDB

Page 4: Building Your First App with Shawn Mcarthy

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

Page 5: Building Your First App with Shawn Mcarthy

5

Terminology

RDBMS MongoDB

Table, View ➜ Collection

Row ➜ Document

Index ➜ Index

Join ➜ Embedded Document

Foreign Key ➜ Reference

Partition ➜ Shard

Page 6: Building Your First App with Shawn Mcarthy

6

Open Source

• MongoDB is an open source project• https://www.github.com/mongodb• Started & sponsored by MongoDB, Inc.• Licensed under the AGPL• Commercial licenses available• Contributions welcome

Page 7: Building Your First App with Shawn Mcarthy

7

Horizontally Scalable

Page 8: Building Your First App with Shawn Mcarthy

8

Database Landscape

Page 9: Building Your First App with Shawn Mcarthy

9

Full Featured

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

Page 10: Building Your First App with Shawn Mcarthy

10

http://www.mongodb.org/downloads

1

2

Page 11: Building Your First App with Shawn Mcarthy

11

Page 12: Building Your First App with Shawn Mcarthy

Shawns-MacBook-Pro:~ shawn$ tar xvf ./Downloads/mongodb-osx-x86_64-3.0.4.tgz x mongodb-osx-x86_64-3.0.4/READMEx mongodb-osx-x86_64-3.0.4/THIRD-PARTY-NOTICESx mongodb-osx-x86_64-3.0.4/GNU-AGPL-3.0x mongodb-osx-x86_64-3.0.4/bin/mongodumpx mongodb-osx-x86_64-3.0.4/bin/mongorestorex mongodb-osx-x86_64-3.0.4/bin/mongoexportx mongodb-osx-x86_64-3.0.4/bin/mongoimportx mongodb-osx-x86_64-3.0.4/bin/mongostatx mongodb-osx-x86_64-3.0.4/bin/mongotopx mongodb-osx-x86_64-3.0.4/bin/bsondumpx mongodb-osx-x86_64-3.0.4/bin/mongofilesx mongodb-osx-x86_64-3.0.4/bin/mongooplogx mongodb-osx-x86_64-3.0.4/bin/mongoperfx mongodb-osx-x86_64-3.0.4/bin/mongosniffx mongodb-osx-x86_64-3.0.4/bin/mongodx mongodb-osx-x86_64-3.0.4/bin/mongosx mongodb-osx-x86_64-3.0.4/bin/mongo

Unpacking the Tarball

Page 13: Building Your First App with Shawn Mcarthy

$ cd mongodb-osx-x86_64-3.0.3/bin

$ sudo mkdir –p /data/db

$ sudo chown -R shawn:staff /data

$ ./mongod

Running MongoDB

Page 14: Building Your First App with Shawn Mcarthy

Shawns-MacBook-Pro:bin shawn$ ./mongod2015-07-18T18:36:48.814-0400 I JOURNAL [initandlisten] journal dir=/data/db/journal2015-07-18T18:36:48.815-0400 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed2015-07-18T18:36:48.836-0400 I JOURNAL [durability] Durability thread started2015-07-18T18:36:48.836-0400 I JOURNAL [journal writer] Journal writer thread started2015-07-18T18:36:48.837-0400 I CONTROL [initandlisten] MongoDB starting : pid=27923 port=27017 dbpath=/data/db 64-bit host=Shawns-MacBook-Pro.local2015-07-18T18:36:48.837-0400 I CONTROL [initandlisten] 2015-07-18T18:36:48.837-0400 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 10002015-07-18T18:36:48.837-0400 I CONTROL [initandlisten] db version v3.0.42015-07-18T18:36:48.837-0400 I CONTROL [initandlisten] git version: 0481c958daeb2969800511e7475dc66986fa9ed52015-07-18T18:36:48.837-0400 I CONTROL [initandlisten] build info: Darwin mci-osx108-11.build.10gen.cc 12.5.0 Darwin Kernel Version 12.5.0: Sun Sep 29 13:33:47 PDT 2013; root:xnu-2050.48.12~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_492015-07-18T18:36:48.837-0400 I CONTROL [initandlisten] allocator: system2015-07-18T18:36:48.837-0400 I CONTROL [initandlisten] options: {}2015-07-18T18:36:48.842-0400 I INDEX [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes...2015-07-18T18:36:48.899-0400 I STORAGE [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...2015-07-18T18:36:48.899-0400 I STORAGE [FileAllocator] creating directory /data/db/_tmp2015-07-18T18:36:49.070-0400 I STORAGE [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB, took 0.17 secs2015-07-18T18:36:49.196-0400 I NETWORK [initandlisten] waiting for connections on port 27017

Log Output from mongod

Page 15: Building Your First App with Shawn Mcarthy

Shawns-MacBook-Pro:bin shawn$ ./mongo

MongoDB shell version: 3.0.4

connecting to: 127.0.0.1:7=27017/test

> db.names.insert({'fullname':’Shawn McCarthy’})

WriteResult({ "nInserted" : 1 })

> db.names.findOne()

{

"_id" : ObjectId("55aad5d2795e9e59af9b375d"),

"fullname" : ”Shawn McCarthy",

}

>

Inserting Your First Document

Page 16: Building Your First App with Shawn Mcarthy

16

Web Demo

• MongoDB• Python• Bottle web framework• Pymongo

Page 17: Building Your First App with Shawn Mcarthy

$ sudo easy_install pip

$ sudo pip install pymongo

$ sudo pip install bottle

Python Prerequisites

Page 18: Building Your First App with Shawn Mcarthy

from pymongo import MongoClientfrom bottle import route, run, template

@route('/')def index(): collection = db.names doc = 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

Connect to the MongoDB Database on Localhost and use the “test” database

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

Start the webserver on locahost, listening on port 8080

Page 19: Building Your First App with Shawn Mcarthy

Our First App

Page 20: Building Your First App with Shawn Mcarthy

Let’s Design a Blog

Page 21: Building Your First App with Shawn Mcarthy

Determine Your Entities

First Step In Your App

Page 22: Building Your First App with Shawn Mcarthy

22

Entities in our Blogging System

• Users (post authors)• Posts • Comments• Tags

Page 23: Building Your First App with Shawn Mcarthy

We Would Start By Doing Schema Design

In a relational based app

Page 24: Building Your First App with Shawn Mcarthy

24

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 25: Building Your First App with Shawn Mcarthy

In MongoDB We Start By Building Our App

And Let The Schema Evolve

Page 26: Building Your First App with Shawn Mcarthy

26

MongoDB ERD

titlebodydateusername

Posts

[ ] comments

[ ] tags

Usernamepasswordemail

Users

Page 27: Building Your First App with Shawn Mcarthy

Manipulating Blog Data

(mongo shell version)

Page 28: Building Your First App with Shawn Mcarthy

user = {

_id: ’mccarthy',

password: ‘XXXXX’,

email: ’[email protected]

}

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

Page 29: Building Your First App with Shawn Mcarthy

> db.users.insert(user)

Insert the record

No collection creation needed

Page 30: Building Your First App with Shawn Mcarthy

> db.users.findOne()

{

"_id" : ”mccarthy",

"password" : ”XXXXX",

"email" : “[email protected]

}

Querying for the user

Page 31: Building Your First App with Shawn Mcarthy

> db.posts.insert({

title: ‘Hello World’,

body: ‘This is my first blog post’,

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

username: ‘mccarthy’,

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

comments: []

})

Creating a blog post

Page 32: Building Your First App with Shawn Mcarthy

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" : ”mccarthy","tags" : [

"adventure","mongodb"

],"comments" : [ ]

}

Finding the Post

Page 33: Building Your First App with Shawn Mcarthy

> 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" : ”mccarthy","tags" : [

"adventure","mongodb"

],"comments" : [ ]

}

Querying an Array

Page 34: Building Your First App with Shawn Mcarthy

> db.posts.update(

{_id: new ObjectId("51c3bcddfbd5d7261b4cdb5b")},

{$push:{comments:

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

Using Update to Add a Comment

Predicate of the query. Specifies which document to update

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

Page 35: Building Your First App with Shawn Mcarthy

> 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" : ”mccarthy"

}

Post with Comment Attached

Page 36: Building Your First App with Shawn Mcarthy

MongoDB Drivers

Page 37: Building Your First App with Shawn Mcarthy

37

Page 38: Building Your First App with Shawn Mcarthy
Page 39: Building Your First App with Shawn Mcarthy

Next Steps

Page 40: Building Your First App with Shawn Mcarthy

40

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

Page 41: Building Your First App with Shawn Mcarthy
Page 42: Building Your First App with Shawn Mcarthy