Building Your First Application with MongoDB

Preview:

DESCRIPTION

 

Citation preview

VP, Education & Cloud Services, 10genAndrew Erlichson

#MongoDBDays

Building your first app;an introduction to MongoDB

What is 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

Open Source• MongoDB is an open source project• On GitHub• Licensed under the AGPL• Started & sponsored by 10gen• Commercial licenses available• Contributions welcome

Horizontally Scalable

Database Landscape

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

http://www.mongodb.org/downloads

$ 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

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

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

DocumentForeign Key ➜ ReferencePartition ➜ ShardTerminology

Let’s Build a Blog

First step in any application isDetermine your entities

Entities in our Blogging System

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

In a relational based appWe would start by doing schema design

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

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

MongoDB ERD

titlebodydateusername

Posts

[ ] comments

[ ] tags

usernamefullnameemail

users

No common languageso using the shell

Working with MongoDB

user = { username:

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

’Erlichson',}

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

> db.users.insert(user)

Insert the record

No collection creation needed

> db.users.findOne(){

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

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

}

Querying for the user

> 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

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

> 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

> db.posts.update({_id:

new ObjectId("51c3bcddfbd5d7261b4cdb5b")},

{$push:{comments:

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

Post'}}})>

Using Update to Add a Comment

> 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

MongoDB Drivers

http://api.mongodb.org/

Next Steps

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

July 29 June 17 July 15

Schema Design @ 11:05 am

Replication @ 11:55 am

Indexing @ 12:40 pm

Sharding – view online

VP, Education & Cloud Services, 10genAndrew Erlichson (andrew@10gen.com)

#MongoDBDays

Questions?

• Schema Design @ 11:05am

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