Advertisement

Building Your First MongoDB App ~ Metadata Catalog

May. 19, 2012
Advertisement

More Related Content

Advertisement

Building Your First MongoDB App ~ Metadata Catalog

  1. Building Your First Application Kevin Hanson Solutions Architect, 10gen Kevin@10gen.com @hungarianhc
  2. Step 1 - What is mongoDB?? • Scalable, High-Performance, Open Source, Document- Oriented Database – JSON (well... BSON) Storage Model – Indexes and Full Query Language – Easy for Developers to Pick Up • More Features at http://www.mongodb.org/ • Overview Video: http://www.10gen.com/what-is-mongodb
  3. Step 2 - Stop Thinking Relational
  4. Tables to Documents { title: ‘MongoDB’, contributors: [ { name: ‘Eliot Horowitz’, email: ‘eliot@10gen.com’ }, { name: ‘Dwight Merriman’, email: ‘dwight@10gen.com’ } ], model: { relational: false, awesome: true } }
  5. Parallels RDBMS MongoDB Table Collection Row Document Column Field Index Index Join Embedding / Linking Schema Object
  6. Rich Data Model { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : "Spirited Away", tags : [ "Tezuka", "Manga" ], comments : [ { author : "Fred", date : "Sat Jul 24 2010 20:51:03 GMT-0700 (PDT)", text : "Best Movie Ever" } ]}
  7. Querying >db.posts.find() { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : "Spirited Away", tags : [ "Tezuka", "Manga" ] } Notes: - _id is unique, but can be anything you’d like
  8. Secondary Indexes Create index on any Field in Document // 1 means ascending, -1 means descending >db.posts.ensureIndex({author: 1}) >db.posts.find({author: 'roger'}) { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", ... }
  9. Secondary Indexes // Index nested documents > db.posts.ensureIndex( “comments.author”:1 ) db.posts.find({‘comments.author’:’Fred’}) // Index on tags > db.posts.ensureIndex( tags: 1) > db.posts.find( { tags: ’Manga’ } ) // geospatial index > db.posts.ensureIndex( “author.location”: “2d” ) > db.posts.find( “author.location” : { $near : [22,42] } )
  10. Atomic Operations • $set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $bit > comment = { author: “fred”, date: new Date(), text: “Best Movie Ever”} > db.posts.update( { _id: “...” }, $push: {comments: comment} );
  11. Step 3 - Let’s Build A Sample App Metadata Catalog Q: Patients Matching Query A: Patients Patient Data Metadata
  12. Inserting a Document var p = { name: “John Smith”, address: “578 Broadway, 7th Floor”, city: “New York”, birthday: “2/15/2012”} > db.patients.save(p)
  13. Let’s Add Some Information { name: “John Smith”, address: “578 Broadway, 7th Floor”, city: “New York”, birthday: “2/15/2012”, clinics: [“Good Health Clinic”, “New York General Care”]}
  14. Geo Tags { name: “John Smith”, address: “578 Broadway, 7th Floor”, city: “New York”, birthday: “2/15/2012”, clinics: [“Good Health Clinic”, “New York General Care”], latlong: [40.0, 72.0]}
  15. Doctor Notes { name: “John Smith”, address: “578 Broadway, 7th Floor”, city: “New York”, birthday: “2/15/2012”, clinics: [“Good Health Clinic”, “New York General Care”], latlong: [40.0, 72.0], notes: [{doctor:”Kevin", time:3/15/2012, note:”Patient seems healthy to me!"}]}
  16. Updating Doctor Notes db.places.update({name:”John Smith"}, {$push :{notes: {doctor:”Kevin", time:3/15/2012, note:”Patient seems healthy to me!"}}}}
  17. Indexing / Querying Metadata // List all people whose name is “John Smith” > db.posts.ensureIndex( “name”:1 ) db.posts.find({‘name’:’John Smith’}) // List all people whose name is “John Smith”, sorted by birthday descending > db.posts.ensureIndex( name: 1, birthday: -1) > db.posts.find( { name: ’John Smith’ } ).sort({birthday: -1}) // Return people that “Doctor Bob” made notes about, sorted by their name > db.posts.ensureIndex( notes.doctor: 1, name: 1) > db.posts.find( “notes.doctor”: “Doctor Bob”).sort({name:1})
  18. Demo ~ MongoDB Shell
  19. Step 4 - Scale Your App w/ Replica Sets
  20. Replica Sets
  21. Replica Sets Add Secondaries for High Availability / Read Scale High Write Load / Big Data Set -> Use mongoDB Range Based Auto Sharding (A Future Webinar / mongoDB Day)
  22. More info at http://www.mongodb.org/ conferences, appearances, and meetups http://www.10gen.com/events Kevin Hanson kevin@10gen.com @hungarianhc Facebook | Twitter | LinkedIn http://bit.ly/mongofb @mongodb http://linkd.in/joinmongo
Advertisement