Saturday, March 16, 2013

How to connect two Client IP to the same mongodb using c++ Drivers

I am trying to make a client with mongo c++ drivers to connect to the database. I successfully tested the client for the localhost.The code I used for that is below.

Now I want to have the database and clients on different machines for example client on IP and the mongodb on IP

I changes should I make in the code to achieve that. I tried to change the line

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


     c.connect(""); //"");

But that does not work
The error says "caught can't connect to server " I tried to ping the IP and it give me the response also .

   #include <iostream>
    #include "mongo/client/dbclient.h"
    // g++ src/mongo/client/examples/tutorial.cpp -pthread -Isrc -Isrc/mongo
 -lmongoclient -lboost_thread-mt -lboost_system -lboost_filesystem -L[path to
 libmongoclient.a] -o tutorial
    //g++ tutorial.cpp -L[mongo directory] -L/opt/local/lib -lmongoclient 
-lboost_thread-mt -lboost_filesystem -lboost_system -I/opt/local/include 
 -o tutorial
    using namespace mongo;
    void printIfAge(DBClientConnection& c, int age) {
        auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") );
        while( cursor->more() ) {
            BSONObj p = cursor->next();
            cout << p.getStringField("name") << endl;
    void run() {
        DBClientConnection c;
        c.connect("localhost"); //"");
        cout << "connected ok" << endl;
        BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
        c.insert("tutorial.persons", p);
        p = BSON( "name" << "Jane" << "age" << 40 );
        c.insert("tutorial.persons", p);
        p = BSON( "name" << "Abe" << "age" << 33 );
        c.insert("tutorial.persons", p);
        p = BSON( "name" << "Methuselah" << "age" << BSONNULL);
        c.insert("tutorial.persons", p);
        p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" );
        c.insert("tutorial.persons", p);
        c.ensureIndex("tutorial.persons", fromjson("{age:1}"));
        cout << "count:" << c.count("tutorial.persons") << endl;
        auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());
        while( cursor->more() ) {
            cout << cursor->next().toString() << endl;
        cout << "\nprintifage:\n";
        printIfAge(c, 33);
    int main() {
        try {
        catch( DBException &e ) {
            cout << "caught " << e.what() << endl;
        return 0;

I found the Answer of the above question

Earlier the mongod was running on the defined port and path on the Mongodb server side 
So I stoped the Mongodb by following command

    $ sudo stop mongodb

and once again started the server with the default defined path by the following command

    $ sudo mongod --dbpath /data/db --port 27017 

and then tried the above code in c++ and this time it connected to the remote server.
IT Certification Category (English)640x480

Partner Sites

Top Online Courses From ProgrammingKnowledge

Python Course
Java Course
Bash Course
Linux Course
C Course
C++ Course
PHP Course
Android Course
C# Course
JavaFx Course
NodeJs Course
Jenkins Course
Scala Course
Bootstrap Course
MongoDB Course
QT C++ GUI Course