36
Consulting Engineer, 10gen Norman Graham Building your first app: an introduction to MongoDB

Webinar: Building Your First App

  • Upload
    mongodb

  • View
    105

  • Download
    1

Embed Size (px)

DESCRIPTION

This talk will introduce the features of MongoDB by walking through how one can building a simple location-based checkin application using MongoDB. The talk will cover the basics of MongoDB's document model, query language, map-reduce framework and deployment architecture.

Citation preview

Page 1: Webinar: Building Your First App

Consulting Engineer, 10gen

Norman Graham

Building your first app:an introduction to MongoDB

Page 2: Webinar: Building Your First App

What is MongoDB

Page 3: Webinar: Building Your First App

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 4: Webinar: Building Your First App

Open Source

• MongoDB is an open source project

• On GitHub

• Licensed under the AGPL

• Started & sponsored by 10gen

• Commercial licenses available

• Contributions welcome

Page 5: Webinar: Building Your First App

Horizontally Scalable

Page 6: Webinar: Building Your First App

Database Landscape

Page 7: Webinar: Building Your First App

Full Featured

• Ad Hoc queries

• Real time aggregation

• Rich query capabilities

• Strongly consistent

• Geospatial features

• Support for most programming languages

• Flexible schema

Page 8: Webinar: Building Your First App

http://www.mongodb.org/downloads

Page 9: Webinar: Building Your First App

$ tar xvf mongodb-osx-i386-2.4.4.tar.gz

$ cd mongodb-osx-i386-2.4.4/bin

$ mkdir –p /data/db

$ ./mongod

Running MongoDB

Page 10: Webinar: Building Your First App

$ mongo

MongoDB shell version: 2.4.4

connecting to: test

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

http://docs.mongodb.org/

Questions? Try the support group

http://groups.google.com/group/mongodb-user

> db.test.insert({text: 'Welcome to MongoDB'})

> db.test.find().pretty()

{

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

"text" : "Welcome to MongoDB"

}

Mongo Shell

Page 11: Webinar: Building Your First App

RDBMS MongoDBTable, View ➜ CollectionRow ➜ DocumentIndex ➜ IndexJoin ➜ Embedded

DocumentForeign Key ➜ ReferencePartition ➜ Shard

Terminology

Page 12: Webinar: Building Your First App

Let’s Build a Blog

Page 13: Webinar: Building Your First App

First step in any application isDetermine your entities

Page 14: Webinar: Building Your First App

Entities in our Blogging System

• Users (post authors)

• Posts

• Comments

• Tags

Page 15: Webinar: Building Your First App

In a relational based appWe would start by doing schema design

Page 16: Webinar: Building Your First App

Typical (relational) ERD

tag_idtag

tags

post_idpost_titlebodypost_datepost_author_uid

post_idcomment_idcommentauthor_namecomment_dateauthor_email

uidusernamefullnameemail

post_idtag_id

users

posts comments

post_tags

Page 17: Webinar: Building Your First App

In a MongoDB based appWe start building our appand let the schema evolve

Page 18: Webinar: Building Your First App

MongoDB ERD

titlebodydateusername

Posts

[ ] comments

[ ] tags

usernamefullnameemail

users

Page 19: Webinar: Building Your First App

No common languageso using the shell

Page 20: Webinar: Building Your First App

Working with MongoDB

Page 21: Webinar: Building Your First App

user = {

username: 'ngraham',

first_name: 'Norman',

last_name: 'Graham’

}

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

Page 22: Webinar: Building Your First App

> db.users.insert(user)

Insert the record

No collection creation needed

Page 23: Webinar: Building Your First App

> db.users.findOne()

{

"_id" : ObjectId("50804d0bd94ccab2da652599"),

"username" : "ngraham",

"first_name" : "Norman",

"last_name" : "Graham"

}

Querying for the user

Page 24: Webinar: Building Your First App

> db.posts.insert({

title: "Hello World",

body: "This is my first blog post",

date: new Date("2013-06-20"),

username: "ngraham",

tags: ["adventure", "mongodb"],

comments: []

})

Creating a blog post

Page 25: Webinar: Building Your First App

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

"adventure","mongodb"

],"comments" : [ ]

}

Finding the Post

Page 26: Webinar: Building Your First App

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

"adventure","mongodb"

],"comments" : [ ]

}

Querying an Array

Page 27: Webinar: Building Your First App

> db.posts.update({_id:

new ObjectId("51c3bcddfbd5d7261b4cdb5b")},

{$push:{comments:

{name: 'Steve Blank', comment: 'Awesome

Post'}}})>

Using Update to Add a Comment

Page 28: Webinar: Building Your First App

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

}

Post with Comment Attached

Page 29: Webinar: Building Your First App

MongoDB Drivers

Page 30: Webinar: Building Your First App
Page 31: Webinar: Building Your First App
Page 32: Webinar: Building Your First App

http://api.mongodb.org/

Page 33: Webinar: Building Your First App

Next Steps

Page 34: Webinar: Building Your First App

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

Page 35: Webinar: Building Your First App
Page 36: Webinar: Building Your First App

Consulting Engineer, 10gen

Norman Graham ([email protected])

Questions?