Building Your First App with Shawn Mcarthy
-
Upload
mongodb -
Category
Technology
-
view
183 -
download
1
Transcript of Building Your First App with Shawn Mcarthy
Building Your First App With MongoDB
Shawn McCarthy
Solutions Architect
What is MongoDB
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
5
Terminology
RDBMS MongoDB
Table, View ➜ Collection
Row ➜ Document
Index ➜ Index
Join ➜ Embedded Document
Foreign Key ➜ Reference
Partition ➜ Shard
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
7
Horizontally Scalable
8
Database Landscape
9
Full Featured
• Ad Hoc queries• Real time aggregation• Rich query capabilities• Geospatial features• Support for most programming languages• Flexible schema
10
http://www.mongodb.org/downloads
1
2
11
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
$ cd mongodb-osx-x86_64-3.0.3/bin
$ sudo mkdir –p /data/db
$ sudo chown -R shawn:staff /data
$ ./mongod
Running MongoDB
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
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
16
Web Demo
• MongoDB• Python• Bottle web framework• Pymongo
$ sudo easy_install pip
$ sudo pip install pymongo
$ sudo pip install bottle
Python Prerequisites
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
Our First App
Let’s Design a Blog
Determine Your Entities
First Step In Your App
22
Entities in our Blogging System
• Users (post authors)• Posts • Comments• Tags
We Would Start By Doing Schema Design
In a relational based app
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
In MongoDB We Start By Building Our App
And Let The Schema Evolve
26
MongoDB ERD
titlebodydateusername
Posts
[ ] comments
[ ] tags
Usernamepasswordemail
Users
Manipulating Blog Data
(mongo shell version)
user = {
_id: ’mccarthy',
password: ‘XXXXX’,
email: ’[email protected]’
}
Start with an object (or array, hash, dict, etc)
> db.users.insert(user)
Insert the record
No collection creation needed
> db.users.findOne()
{
"_id" : ”mccarthy",
"password" : ”XXXXX",
"email" : “[email protected]”
}
Querying for the user
> 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
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
> 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
> 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
> 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
MongoDB Drivers
37
Next Steps
40
http://docs.mongodb.org/ecosystem/drivers/