MongoDB NoSQL and all of its awesomeness


Published 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 at:

Published in: Education, Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

MongoDB NoSQL and all of its awesomeness

  1. 1. MongoDB and NoSQL ...and all of its awesomeness....
  2. 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. 3. Terminology● db > collection > document● collection = table● document = row● Database is made of of collections● Collections are made up of documents
  4. 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. 5. Why to Not Use MongoDB (Cons)● Non transactional● Document Size Limitations 16MB● Ambiguous keys when shortened
  6. 6. MongoDB SyntaxINSERT > > >, updates, upsert, multi)DELETE >
  7. 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. 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. 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. 10. DemoEnough some freaking code...
  11. 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. 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. 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. 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. 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. 16. Hope you Enjoyed!twitter: @troygrosfieldwebsite: http://troygrosfield.comblog: Resources:●