40
VP, Education & Cloud Services, 10gen Andrew Erlichson #MongoDBDays Building your first app; an introduction to MongoDB

Building Your First Application with MongoDB

  • Upload
    mongodb

  • View
    2.535

  • Download
    7

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Building Your First Application with MongoDB

VP, Education & Cloud Services, 10genAndrew Erlichson

#MongoDBDays

Building your first app;an introduction to MongoDB

Page 2: Building Your First Application with MongoDB

What is MongoDB

Page 3: Building Your First Application 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 4: Building Your First Application with MongoDB

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: Building Your First Application with MongoDB

Horizontally Scalable

Page 6: Building Your First Application with MongoDB

Database Landscape

Page 7: Building Your First Application 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 8: Building Your First Application with MongoDB

http://www.mongodb.org/downloads

Page 9: Building Your First Application with MongoDB

$ 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: Building Your First Application with MongoDB

Andrews-MacBook-Air-2:~ aje$ mongoMongoDB shell version: 2.4.4connecting to: testWelcome 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: Building Your First Application with MongoDB

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

DocumentForeign Key ➜ ReferencePartition ➜ ShardTerminology

Page 12: Building Your First Application with MongoDB

Let’s Build a Blog

Page 13: Building Your First Application with MongoDB

First step in any application isDetermine your entities

Page 14: Building Your First Application with MongoDB

Entities in our Blogging System

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

Page 15: Building Your First Application with MongoDB

In a relational based appWe would start by doing schema design

Page 16: Building Your First Application with MongoDB

Typical (relational) ERD

tag_idtag

tags

post_idpost_titlebodypost_datepost_author_uid

post_idcomment_idcommentauthor_namecomment_dateauthor_email

uidusernamefullnameemail

post_idtag_id

usersposts comments

post_tags

Page 17: Building Your First Application with MongoDB

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

Page 18: Building Your First Application with MongoDB

MongoDB ERD

titlebodydateusername

Posts

[ ] comments

[ ] tags

usernamefullnameemail

users

Page 19: Building Your First Application with MongoDB

No common languageso using the shell

Page 20: Building Your First Application with MongoDB

Working with MongoDB

Page 21: Building Your First Application with MongoDB

user = { username:

’erlichson', first_name: ’Andrew',last_name:

’Erlichson',}

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

Page 22: Building Your First Application with MongoDB

> db.users.insert(user)

Insert the record

No collection creation needed

Page 23: Building Your First Application with MongoDB

> db.users.findOne(){

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

"username" : ”erlichson","first_name" : ”Andrew","last_name" : ”Erlichson"

}

Querying for the user

Page 24: Building Your First Application 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 25: Building Your First Application 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 26: Building Your First Application 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 27: Building Your First Application with MongoDB

> db.posts.update({_id:

new ObjectId("51c3bcddfbd5d7261b4cdb5b")},

{$push:{comments:

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

Post'}}})>

Using Update to Add a Comment

Page 28: Building Your First Application 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 29: Building Your First Application with MongoDB

MongoDB Drivers

Page 30: Building Your First Application with MongoDB
Page 31: Building Your First Application with MongoDB
Page 32: Building Your First Application with MongoDB

http://api.mongodb.org/

Page 33: Building Your First Application with MongoDB

Next Steps

Page 34: Building Your First Application with MongoDB

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

Page 35: Building Your First Application with MongoDB

July 29 June 17 July 15

Page 36: Building Your First Application with MongoDB

Schema Design @ 11:05 am

Page 37: Building Your First Application with MongoDB

Replication @ 11:55 am

Page 38: Building Your First Application with MongoDB

Indexing @ 12:40 pm

Page 39: Building Your First Application with MongoDB

Sharding – view online

Page 40: Building Your First Application with MongoDB

VP, Education & Cloud Services, 10genAndrew Erlichson ([email protected])

#MongoDBDays

Questions?

• Schema Design @ 11:05am

• Replication @ 11:55am• Indexing @ 12:40pm• Sharding - online