Presented at Cerner's web dev meetup about what MongoDB is as well as give some examples on how to use MongoDB from the Mongo shell. See the video at:
http://blog.troygrosfield.com/2012/03/30/web-developer-meetup-presentation-mongodb-and-nosql/
2. What is MongoDB?
● MongoDB (from 'humongous') is a scalable,
high-performance, open source NoSQL
database.
● JSON-style document storage
○ actually BSON (Binary JSON)
● Awesome
3. Terminology
● db > collection > document
● collection = table
● document = row
● Database is made of of collections
● Collections are made up of documents
4. Why Should I Use MongoDB (Pros)
● Flexible: database changes are easy
because it's a dynamic schema.
● Fast: Indexes are stored in memory which
alleviates need for additional cache
dependency
● Easy to scale with replica sets, sharding
● Atomic updates: All fail or all succeed
5. Why to Not Use MongoDB (Cons)
● Non transactional
● Document Size Limitations 16MB
● Ambiguous keys when shortened
7. Blog Post Example
● Create a Blog Post
● Retrieve a Blog Post
● Update a Blog Post
○ $inc, $set, $unset, $push, $pushAll, $addToSet and
$each, $pop, $pull, $pullAll, $rename
● Delete a Blog Post
8. Define the Model
class BlogPost(): In SQL world, most of you already can
id see this would require 3 separate
name tables:
content
1. BlogPosts
author_id 2. Comments
3. Tags
class Comments():
author_id
comment
blog_id
class Tags():
name
blog_id
9. Mongo Document Storage Structure
Document structure inside of the "BlogPost" collection:
{
'id': 12345,
'name': 'Hello World',
'author_id': 112233,
'content': 'This is my blog post content. Woot woot!',
'comments': [
{'user_id': 4321, 'comment': 'Awesome blog!'},
{'user_id': 1234, 'comment': 'Totally Agree'}
],
'tags': ['hello', 'world']
}
11. Demo Code: Insert
doc2 = {'id': 67890,
doc = {'id': 12345,
'name': 'Another Hello World',
'name': 'Hello World',
'author_id': 112233,
'author_id': 112233,
'content': 'This is my second blog post',
'content': 'This is my blog post content. Woot woot!',
'comments': [
'comments': [
{'user_id': 4321, 'comment': 'Even better than
{'user_id': 4321, 'comment': 'Awesome blog!'},
the first!'},
{'user_id': 1234, 'comment': 'Totally Agree'}
{'user_id': 1234, 'comment': 'You rock'}
],
],
'tags': ['hello', 'world']
'tags': ['jimmy', 'buffet']
}
}
db.blogpost.insert(doc)
db.blogpost.insert(doc2)
12. Demo Code: Retrieve
Return the whole doc:
● db.blogpost.findOne()
● db.blogpost.findOne({'id': 12345})
Only return specific fields:
● db.blogpost.findOne({}, {'name':1, 'author_id':1})
Exclude specific fields:
● db.blogpost.findOne({}, {'comments': 0})
Find all blog posts by author 112233:
● db.blogpost.find({'id': 112233}).pretty()
13. Demo Code: Update
Update all posts by Author_id:
● db.blogpost.update({'author_id': 112233},
{'$set':{'is_featured': true}}, false, true)
Update all posts by author_id using tag "buffet":
● db.blogpost.update({'author_id': 112233, 'tags': 'buffet'},
{'$set':{'rock_star': true}}, false, true)
Update all posts by author_id where comment == 'You rock':
● db.blogpost.update({'author_id': 112233, 'comments.comment': 'You rock'},
{'$set':{'comments.$.about_rock': true}}, false, true)
Add tag to all posts by author_id:
● db.blogpost.update({'author_id': 112233},
{'$addToSet':{'tags': 'word up'}}, false, true)
14. Demo Code: Delete
Remove by document id:
● db.blogpost.remove({'id': 12345})
Remove all posts by author_id:
● db.blogpost.remove({'author_id': 112233})
Remove all blog posts:
● db.blogpost.remove({})
15. Tips
● Use small keys
○ a collection with 1,000,000 documents with keys like:
{'first_name': 'Troy', 'last_name': 'Grosfield'}
will take longer to search than a 1,000,000 documents with small
keys such as:
{'fn': 'Troy', 'ln': 'Grosfield'}
● Be smart with your indexes
● Design documents wisely