MongoDB NoSQL and all of its awesomeness

  • 9,722 views
Uploaded on

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 …

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/

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
9,722
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
3
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. MongoDB and NoSQL ...and all of its awesomeness....
  • 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 its 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
  • 6. MongoDB SyntaxINSERT > db.foo.insert(doc_or_docs)SELECT > db.foo.find(criteria)UPDATE > db.foo.update(criteria, updates, upsert, multi)DELETE > db.foo.remove(criteria)
  • 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 Modelclass 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. Tagsclass Comments(): author_id comment blog_idclass Tags(): name blog_id
  • 9. Mongo Document Storage StructureDocument 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]}
  • 10. DemoEnough talking...show some freaking code...
  • 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: RetrieveReturn 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: UpdateUpdate 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: DeleteRemove 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
  • 16. Hope you Enjoyed!twitter: @troygrosfieldwebsite: http://troygrosfield.comblog: http://blog.troygrosfield.com Resources:● http://mongodb.org/