Mongo Poco Subsystem

Mongo DB client adapter for the POCO project C++ framework.


Welcome to mongosubsystem Download the current release (0.1, Feb-2011) Contact the author

What is it?

Poco is a C++ framework for building portable applications. MongoDB is a Document oriented database. MongoSubsystem is a Poco subsystem that is a native client for MongoDB.

Installation

  1. Download and install POCO On debian:
    aptitude install poco-dev
  2. Download and install MongoDB On debian:
    aptitude install mongodb
  3. Download the current release (0.1, Feb-2011).
  4. Extract the sources. On debian:
    tar -xvzf mongosubsystem-0.1.tar.gz
  5. Build and install the package. On debian:
    configure
    make
    sudo make install

The package installs mongosubsystem.so and include/Mongo header files.

Example

Note: the example package can be downloaded from here.
#include <iostream>
#include <Mongo/MongoSubsystem.h>
#include <Mongo/MongoConnection.h>
#include <Mongo/MongoQuery.h>
#include <Poco/Util/Application.h>

using namespace std;

class AppMain : public Poco::Util::Application {
public:
    AppMain();
    ~AppMain();
protected:
    virtual int main(const std::vector& args);
private:
    void configureConnection();
};

AppMain::AppMain() {
    addSubsystem(new Mongo::MongoSubsystem());
}

AppMain::~AppMain() {

}

void AppMain::configureConnection() {
    config().setString("mongo.host.ip", string("127.0.0.1"));
    config().setInt("mongo.host.port", 27017);
    config().setString("mongo.dbName", string("test"));
}

int AppMain::main(const std::vector& args) {
    configureConnection();

    cout << endl << endl;

    //create document
    Mongo::MongoDocument personDoc("person");
    personDoc.setValue("legs", 4);
    personDoc.setValue("eyes", string("brown"));
    personDoc.setValue("human", false);
    Mongo::MongoDocument homeDoc;
    homeDoc.setValue("lives in", string("appartment"));
    personDoc.setObject("home", homeDoc.ptr());

    // insert
    Mongo::MongoSubsystem& mongo = getSubsystem ();
    Mongo::ConnectionPool::MongoConnectionPtr connection =
         mongo.defaultConnectionPool().getConnection();
    for (int i = 0; i < 10; ++i) {
        homeDoc.setValue("number", i);
        connection->insert(personDoc);
        cout << "inserting" << personDoc.toString() 
             << " to collection 'person'" << endl;
    }
    cout << endl << endl;
    //find the doc with "legs = 4"
    Mongo::MongoDocument query("person");
    query.setValue("legs", 4);
    Mongo::MongoDocument answer;
    if (connection->findOne(query, answer)) {
        cout << "looking for " << query.toString()
         << " found " << answer.toString() << endl;
    }

    // find the doc with a specific child
    homeDoc.setValue("number", 5);
    query.setObject("home", homeDoc.ptr());
    if (connection->findOne(query, answer)) {
        cout << "looking for " << query.toString()
        << " found " << answer.toString() << endl;
    }


    // remove the collection
    connection->erase(Mongo::MongoDocument("person"));

    connection->close();

}


POCO_APP_MAIN(AppMain)
        

The example is compiled with:

g++ -o pocomongo PocoMongo.cpp -lPocoFoundation \
-lPocoUtil -lmongosubsystem -lPocoNet