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
Consulting Engineer, 10gen
Norman Graham
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
$ 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
RDBMS MongoDBTable, View ➜ CollectionRow ➜ DocumentIndex ➜ IndexJoin ➜ Embedded
DocumentForeign Key ➜ ReferencePartition ➜ Shard
Terminology
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
users
posts 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: 'ngraham',
first_name: 'Norman',
last_name: 'Graham’
}
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" : "ngraham",
"first_name" : "Norman",
"last_name" : "Graham"
}
Querying for the user
> 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
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
> 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
> 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" : ”ngraham"
}
Post with Comment Attached
MongoDB Drivers
http://api.mongodb.org/
Next Steps
http://docs.mongodb.org/manual/