c++ driver: DBClientReplicaSet insert() Throws Exceptions

218 views
Skip to first unread message

gli

unread,
Oct 26, 2012, 5:27:33 PM10/26/12
to mongo...@googlegroups.com

Hi,

I used the rs command in mongo command shell and created a replica set, with 3 members. rs.status() command shows the 3 members, with one "PRIMARY" and the rest two "SECONDARY". All the "health" flags are 1.

I followed the mongo-cxx-driver-v2.2/src/mongo/client/examples/rs.cpp, and wrote a mongo client replica program. The following code calls the connect(), and it works:

string errmsg;
ConnectionString cs = ConnectionString::parse( "server1:27017,server2:27017, server3:27017" , errmsg );

DBClientReplicaSet * conn = static_cast<DBClientReplicaSet*>( cs.connect( errmsg, 30 ) );

The c++ driver outputs the following messages to stdout, after the connect() call:

Fri Oct 26 13:05:12 SyncClusterConnection connecting to [server1:27017]
Fri Oct 26 13:05:13 SyncClusterConnection connecting to [server2:27017]
Fri Oct 26 13:05:13 SyncClusterConnection connecting to [server3:27017]

However, the insert() throws exception, with the text of "SyncClusterConnection::insert obj has to have an _id: {"...

This is how a BSONObj is built:

mongo::BSONObjBuilder builder;
builder.append("tranname", value1);
builder.append("tranid", value2);
builder.append("time", value3);
builder.append("xml", value4);

mongo::BSONObj bson = builder.obj();
conn->insert("collection_name", bson);

I guess the problem is the way how BSONObj is built for DBClientReplicaSet. The BSONObj built works for DBClientConnection.
 
Could anyone please shed the light?
 

Thanks for your help.

gli

Dwight Merriman

unread,
Nov 1, 2012, 12:28:38 PM11/1/12
to mongo...@googlegroups.com
i don't know the exact answer but it looks like you are not getting a DBClientReplicaSet object but rather the syncclusterconnection thing. 

perhaps instantiates directly? ie:

 DBClientReplicaSet * set = new DBClientReplicaSet( _setName , _servers , socketTimeout );
            if( ! set->connect() ) {
                delete set;
                errmsg = "connect failed to set ";
                errmsg += toString();
                return 0;
            }
            return set;

see client/dbclient.cpp for more details including the parse() method source.

p.s. without diving deeper i don't know why it does that but it does...

Dwight Merriman

unread,
Nov 1, 2012, 12:30:58 PM11/1/12
to mongo...@googlegroups.com
i think i see now

you need <setname>/<hostseedlist>

so you need something like 

  ConnectionString cs = ConnectionString::parse( "MYSETNAME/server1:27017,server2:27017, server3:27017" , errmsg );

 see comment in dbclientinterface.h



On Friday, October 26, 2012 5:27:33 PM UTC-4, gli wrote:

gli

unread,
Nov 2, 2012, 11:47:01 AM11/2/12
to mongo...@googlegroups.com
Hi Dwight,
 
Thanks a lot for pointing out the code in mongo/client/dbclient.cpp. This is exact call I'm looking for.
 
I'll give it a try.
 
GLI
Reply all
Reply to author
Forward
0 new messages