Building Your First MongoDB App

1,012 views
870 views

Published on

Simple introduction to MongoDB via the example of building a blog application.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,012
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
14
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Building Your First MongoDB App

  1. 1. Job Title, MongoDB Speaker Name #ConferenceHashTag Building your first app; an introduction to MongoDB
  2. 2. What is MongoDB?
  3. 3. MongoDB is a ___________ database • Document • Open source • High performance • Horizontally scalable • Full featured
  4. 4. Document Database • Not for .PDF & .DOC files • A document is essentially an associative array – JSON object – PHPArray – Python Dict – RubyHash – etc
  5. 5. var user = { username: ’erlichson', first_name: ’Andrew', last_name: ’Erlichson', } Start with an object (or array, hash, dict, etc)
  6. 6. Database Landscape
  7. 7. Full Featured • Ad Hoc queries • Real time aggregation • Rich query capabilities • Strongly consistent • Geospatial features • Support for most programming languages • Flexible schema
  8. 8. mongodb.org/downloads
  9. 9. $ tar –z xvf mongodb-osx-x86_64-2.4.x.tgz $ cd mongodb-osx-i386-2.4.4/bin $ mkdir –p /data/db $ ./mongod Running MongoDB
  10. 10. MacBook-Air-:~ $ mongo MongoDB shell version: 2.4.4 connecting to: test > db.test.insert({text: 'Welcome to MongoDB'}) > db.test.find().pretty() { "_id" : ObjectId("51c34130fbd5d7261b4cdb55"), "text" : "Welcome to MongoDB" } Mongo Shell
  11. 11. Document Database
  12. 12. Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard
  13. 13. Let’s Build a Blog
  14. 14. First step in any application is Determine your entities
  15. 15. Entities in our Blogging System • Users (post authors) • Article • Comments • Categories, Tags
  16. 16. In a relational base app We would start by doing schema design
  17. 17. Typical (relational) ERD
  18. 18. In a MongoDB based app We start building our app and let the schema evolve
  19. 19. MongoDB ERD
  20. 20. Working With MongoDB
  21. 21. var user = { username: ’erlichson', first_name: ’Andrew', last_name: ’Erlichson', } Start with an object (or array, hash, dict, etc)
  22. 22. >db test > use blog switching to db blog > db.users.insert( user ) Store in DB "blog", collection "users"
  23. 23. >db test > use blog switching to db blog > db.users.insert( user ) Store in DB "blog", collection "users" No collection creation necessary
  24. 24. > db.users.find() { "_id" : ObjectId("50804d0bd94ccab2da652599"), "username" : ”erlichson", "first_name" : ”Andrew", "last_name" : ”Erlichson" } > db.users.find( { "_id" : ObjectId(..) } ) > db.users.find( { "username" : "erlichson" } ) Find a Record
  25. 25. > db.article.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
  26. 26. > db.article.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
  27. 27. > db.article.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
  28. 28. > db.article.update({_id: new ObjectId("51c3bcddfbd5d7261b4cdb5b")}, {$push:{comments: {name: 'Steve Blank', comment: 'Awesome Post'}}}) > Using Update to Add a Comment
  29. 29. > db.article.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
  30. 30. MongoDB Drivers
  31. 31. docs.mongodb.org
  32. 32. Online Training at MongoDB University
  33. 33. Questions?
  34. 34. #ConferenceHashtag Thank You

×