MongoDB For C++ Developers

13,503 views

Published on

A short introduction slide deck about the main classes used to connect between C++ and MongoDB.
Includes code snippets and full example programs.

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

No Downloads
Views
Total views
13,503
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
167
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

MongoDB For C++ Developers

  1. 1. MongoDB For C++ Developers { author: “Ynon Perek” }Saturday, February 2, 13
  2. 2. Whoami Ynon Perek http://ynonperek.com ynon@ynonperek.comSaturday, February 2, 13
  3. 3. Agenda Mongo Is Awesome CRUD Operations The Driver Coding TimeSaturday, February 2, 13
  4. 4. Mongo Is Awesome Data Store for JSON ObjectsSaturday, February 2, 13
  5. 5. Mongo Is Awesome Data Store for JSON Objects { “Name” : “Rose Tyler” }Saturday, February 2, 13
  6. 6. JSON Objects A JSON Object is a collection of key/ value pairs {   "name" : "Rose Tyler", Keys are simple   "race" : "Human", strings   "body parts" : [ "head", "legs"] } Values can be: Numbers, Strings, Arrays, Other Objects, and moreSaturday, February 2, 13
  7. 7. It’s A Document Oriented Data StoreSaturday, February 2, 13
  8. 8. It don’t do joinsSaturday, February 2, 13
  9. 9. It don’t do transactionsSaturday, February 2, 13
  10. 10. Keeping It Simple Document Oriented No Transactions No JoinsSaturday, February 2, 13
  11. 11. Application Architecture APP DBSaturday, February 2, 13
  12. 12. Application Architecture DB APP DB DBSaturday, February 2, 13
  13. 13. What Can Mongo Do For You Create and store objects Arrange them in collections Retrieve them laterSaturday, February 2, 13
  14. 14. Q&ASaturday, February 2, 13
  15. 15. CRUD Operations Create, Read, Update and Destroy DataSaturday, February 2, 13
  16. 16. Mongo CRUD Create is called insert Read is called find Update is called update Destroy is called removeSaturday, February 2, 13
  17. 17. Mongo CRUD From a developer’s perspective, MongoDB operations are the same through the driver and through the console In both cases, operations look like function calls or method invocations We’ll use mongo shell for the rest of this chapterSaturday, February 2, 13
  18. 18. Inserting Data Use the command insert or save to insert a new object db.collection.insert( obj ); db.collection.insert( array );Saturday, February 2, 13
  19. 19. Inserting Data Inserting to a new collection creates the collection Inserting an object with an _id key, it is used as the object’s id (and must be unique).Saturday, February 2, 13
  20. 20. Demo: InsertSaturday, February 2, 13
  21. 21. Reading Data find and findOne perform read operations Both take a query find returns a cursor findOne returns an object Optional: Fields to fetch db.collection.find( <query>, <projection> )Saturday, February 2, 13
  22. 22. Query Document An empty (or missing) query document returns everything db.collection.find({}) db.collection.find()Saturday, February 2, 13
  23. 23. Query Document Each key/value pair in the query document imposes a condition on the results (objects that match). db.movies.find({ “genre” : “indie” }); db.books.find({“pages” : { “$gt” : 100 }});Saturday, February 2, 13
  24. 24. Query Document Query Object Each key/value pair in the query document imposes a condition on the results (objects that match). db.movies.find({ “genre” : “indie” }); db.books.find({“pages” : { “$gt” : 100 }});Saturday, February 2, 13
  25. 25. Query Document A compound query means a logical AND on the conditions. db.inventory.find( { “type” : “snacks”, “available” : { “$lt” : 10 } });Saturday, February 2, 13
  26. 26. Quiz: What Is Returned from alterego publisher Bruce { Earth DC Wayne “publisher” : “DC” } Peter Earth Marvel Parker Krypton Clark Kent DCSaturday, February 2, 13
  27. 27. Quiz: What Is Returned from alterego publisher { Bruce “publisher” : Earth DC Wayne “DC”, “from” : “Earth” Peter Earth Marvel } Parker Krypton Clark Kent DCSaturday, February 2, 13
  28. 28. Resources Queries Cheat Sheet http://www.10gen.com/sites/default/files/downloads/ mongodb_qrc_queries.pdfSaturday, February 2, 13
  29. 29. Demo: QuerySaturday, February 2, 13
  30. 30. Update Update operations modify existing data in the DB Mongo supports two update commands: update() and save() Update is the more general (and complex)Saturday, February 2, 13
  31. 31. Update The general form for update is: db.collection.update( <query>, <update>, <options> ) Which Entries What to do with to update themSaturday, February 2, 13
  32. 32. Update The second argument to update() is an operator object It tells update what to do with the data Some keys you can use: “$set”, “$inc” “$push”, “$pushAll”, “$addToSet”, “$pop”, “$pull”, “$pullAll”Saturday, February 2, 13
  33. 33. Update: set $set modifies a value or add a new value Example: db.posts.update( { title: “Why Is Your Cat Unhappy” }, { $set : { “archived” : true } } );Saturday, February 2, 13
  34. 34. Quiz: $set What happens here ? db.cats.update( { color: “white” }, { “$set” : { “owners” : [“John”, “Jim”] } } );Saturday, February 2, 13
  35. 35. Quiz: $set Update owners array of the first cat with white color If you want to update all objects, use multi db.cats.update( { color: “white” }, { “$set” : { “owners” : [“John”, “Jim”] } } { multi : true } );Saturday, February 2, 13
  36. 36. Deleting Data remove() deletes objects from a collection Takes a query and possibly a <justOne> arguments Examples: db.posts.remove({ “author” : “Father Angelo” }) db.music.remove({ “genres” : “pop” }) db.posts.remove({ “tags” : “funny” }, 1 );Saturday, February 2, 13
  37. 37. Q&ASaturday, February 2, 13
  38. 38. The Driver Hello C++ Mongo C++ Objects Coding TimeSaturday, February 2, 13
  39. 39. Hello Mongo #include <iostream> #include "client/dbclient.h" using namespace mongo;  int main() {   try {   DBClientConnection c;   c.connect("localhost");     cout << "connected ok" << endl;   } catch( DBException &e ) {     cout << "caught " << e.what() << endl;   }   return 0; }Saturday, February 2, 13
  40. 40. Insert Data BSONObjBuilder b; b.append("name", "John"); b.append("age", 19);   BSONObj p1 = b.obj(); c.insert("test.people", p1);Saturday, February 2, 13
  41. 41. Query Data void printIfAge(DBClientConnection&c, int age) {   auto_ptr<DBClientCursor> cursor =     c.query("tutorial.persons", QUERY( "age" << age ) );   while( cursor->more() ) {     BSONObj p = cursor->next();     cout << p.getStringField("name") << endl;   } }Saturday, February 2, 13
  42. 42. Update Data db.update( "tutorial.persons" ,            BSON( "name" << "Joe" << "age" << 33 ),            BSON( "$inc" << BSON( "visits" << 1 ) ) );Saturday, February 2, 13
  43. 43. Delete Data db.remove("tutorial.persons", QUERY("age" << 19));Saturday, February 2, 13
  44. 44. C++ Objects DBClientConnection represents a connection DBClientConnection c; Methods: c.connect("localhost"); connect, auth insert, query, update, removeSaturday, February 2, 13
  45. 45. C++ Objects Query data object represents a query QUERY( "age" << 33 << "school" << "UCLA" ). sort("name"); Methods: QUERY( "age" << GT << 30 << LT << 50 ) sort Create with macro QUERYSaturday, February 2, 13
  46. 46. C++ Objects DBClientCursor Returned from query() auto_ptr<DBClientCursor> cursor = Methods: c.query("tutorial.persons", BSONObj()); more() while( cursor->more() )       cout << cursor->next().toString(); next() Create with auto_ptrSaturday, February 2, 13
  47. 47. Coding Time Mongo Score Board: https://github.com/ ynonp/mongo-score MoChat: https://github.com/ ynonp/mochatSaturday, February 2, 13
  48. 48. Thanks For Listening Ynon Perek Slides at: ynonperek.com Talk to me at: ynon@ynonperek.comSaturday, February 2, 13

×