mongodb for c++ developers

48
MongoDB For C++ Developers { author: “Ynon Perek” } Saturday, February 2, 13

Upload: ynon-perek

Post on 01-Nov-2014

7.852 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: MongoDB For C++ Developers

MongoDB For C++ Developers{ author: “Ynon Perek” }

Saturday, February 2, 13

Page 2: MongoDB For C++ Developers

Whoami

Ynon Perek

http://ynonperek.com

[email protected]

Saturday, February 2, 13

Page 3: MongoDB For C++ Developers

Agenda

Mongo Is Awesome

CRUD Operations

The Driver

Coding Time

Saturday, February 2, 13

Page 4: MongoDB For C++ Developers

Mongo Is Awesome

Data Store for JSON Objects

Saturday, February 2, 13

Page 5: MongoDB For C++ Developers

Mongo Is Awesome

Data Store for JSON Objects

{ “Name” : “Rose Tyler” }

Saturday, February 2, 13

Page 6: MongoDB For C++ Developers

JSON Objects

A JSON Object is a collection of key/value pairs

Keys are simple strings

Values can be: Numbers, Strings, Arrays, Other Objects, and more

{  "name" : "Rose Tyler",  "race" : "Human",  "body parts" : [ "head", "legs"]}

Saturday, February 2, 13

Page 7: MongoDB For C++ Developers

It’s A Document Oriented Data Store

Saturday, February 2, 13

Page 8: MongoDB For C++ Developers

It don’t do joins

Saturday, February 2, 13

Page 9: MongoDB For C++ Developers

It don’t do transactions

Saturday, February 2, 13

Page 10: MongoDB For C++ Developers

Keeping It Simple

Document Oriented

No Transactions

No Joins

Saturday, February 2, 13

Page 11: MongoDB For C++ Developers

Application Architecture

DBAPP

Saturday, February 2, 13

Page 12: MongoDB For C++ Developers

Application Architecture

DBAPP

DB

DB

Saturday, February 2, 13

Page 13: MongoDB For C++ Developers

What Can Mongo Do For You

Create and store objects

Arrange them in collections

Retrieve them later

Saturday, February 2, 13

Page 14: MongoDB For C++ Developers

Q & A

Saturday, February 2, 13

Page 15: MongoDB For C++ Developers

CRUD OperationsCreate, Read, Update and Destroy Data

Saturday, February 2, 13

Page 16: MongoDB For C++ Developers

Mongo CRUD

Create is called insert

Read is called find

Update is called update

Destroy is called remove

Saturday, February 2, 13

Page 17: MongoDB For C++ Developers

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 chapter

Saturday, February 2, 13

Page 18: MongoDB For C++ Developers

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

Page 19: MongoDB For C++ Developers

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

Page 20: MongoDB For C++ Developers

Demo: Insert

Saturday, February 2, 13

Page 21: MongoDB For C++ Developers

find and findOne perform read operations

Both take a query

find returns a cursor

findOne returns an object

db.collection.find( <query>, <projection> )

Reading Data

Optional: Fields to fetch

Saturday, February 2, 13

Page 22: MongoDB For C++ Developers

Query Document

An empty (or missing) query document returns everything

db.collection.find({})

db.collection.find()

Saturday, February 2, 13

Page 23: MongoDB For C++ Developers

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

Page 24: MongoDB For C++ Developers

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 }});

Query Object

Saturday, February 2, 13

Page 25: MongoDB For C++ Developers

Query Document

A compound query means a logical AND on the conditions.

db.inventory.find( { “type” : “snacks”, “available” : { “$lt” : 10 } });

Saturday, February 2, 13

Page 26: MongoDB For C++ Developers

Quiz: What Is Returned

{ “publisher” : “DC”}

from alterego publisher

Earth Bruce Wayne DC

Earth Peter Parker Marvel

Krypton Clark Kent DC

Saturday, February 2, 13

Page 27: MongoDB For C++ Developers

Quiz: What Is Returned

{ “publisher” : “DC”, “from” : “Earth”}

from alterego publisher

Earth Bruce Wayne DC

Earth Peter Parker Marvel

Krypton Clark Kent DC

Saturday, February 2, 13

Page 29: MongoDB For C++ Developers

Demo: Query

Saturday, February 2, 13

Page 30: MongoDB For C++ Developers

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

Page 31: MongoDB For C++ Developers

Update

The general form for update is:

db.collection.update( <query>, <update>, <options> )

Which Entries to update

What to do with them

Saturday, February 2, 13

Page 32: MongoDB For C++ Developers

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

Page 33: MongoDB For C++ Developers

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

Page 34: MongoDB For C++ Developers

Quiz: $set

What happens here ?

db.cats.update( { color: “white” }, { “$set” : { “owners” : [“John”, “Jim”] } });

Saturday, February 2, 13

Page 35: MongoDB For C++ Developers

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

Page 36: MongoDB For C++ Developers

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

Page 37: MongoDB For C++ Developers

Q & A

Saturday, February 2, 13

Page 38: MongoDB For C++ Developers

The Driver

Hello C++ Mongo

C++ Objects

Coding Time

Saturday, February 2, 13

Page 39: MongoDB For C++ Developers

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

Page 40: MongoDB For C++ Developers

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

Page 41: MongoDB For C++ Developers

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

Page 42: MongoDB For C++ Developers

Update Data

db.update( "tutorial.persons" ,           BSON( "name" << "Joe" << "age" << 33 ),           BSON( "$inc" << BSON( "visits" << 1 ) ) );

Saturday, February 2, 13

Page 43: MongoDB For C++ Developers

Delete Data

db.remove("tutorial.persons", QUERY("age" << 19));

Saturday, February 2, 13

Page 44: MongoDB For C++ Developers

C++ Objects

DBClientConnection represents a connection

Methods:

connect, auth

insert, query, update, remove

DBClientConnection c;c.connect("localhost");

Saturday, February 2, 13

Page 45: MongoDB For C++ Developers

C++ Objects

Querydata object represents a query

Methods:

sort

Create with macro QUERY

QUERY( "age" << 33 << "school" << "UCLA" ). sort("name");

QUERY( "age" << GT << 30 << LT << 50 )

Saturday, February 2, 13

Page 46: MongoDB For C++ Developers

C++ Objects

DBClientCursorReturned from query()

Methods:

more()

next()

Create with auto_ptr

auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());

while( cursor->more() )      cout << cursor->next().toString();

Saturday, February 2, 13

Page 48: MongoDB For C++ Developers

Thanks For Listening

Ynon Perek

Slides at:ynonperek.com

Talk to me at: [email protected]

Saturday, February 2, 13