Mongodb connection parameters for C ++ driver

It looks like the C ++ drivers don't accept the uri format for mongodb. No documentation on how I should create the connection string, any guesses?

I need to connect to a replica set with 3 servers and set readPreference parameters.

+3


source to share


4 answers


Create replica set connection in MongoDB C ++ client

Until the problems described in @acm's answer are resolved, I found a workaround for the failed connection strings for the C ++ driver. You can create DBClientReplicaSet

with a vector of hosts and ports like this:

//First create a vector of hosts
//( you can ignore port numbers if yours are default)  

    vector<HostAndPort> hosts;
    hosts.push_back(mongo::HostAndPort("YourHost1.com:portNumber1"));
    hosts.push_back(mongo::HostAndPort("YourHost2.com:portNumber2"));
    hosts.push_back(mongo::HostAndPort("YourHost3.com:portNumber3"));

//Then create a Replica Set DB Client:

    mongo::DBClientReplicaSet connection("YourReplicaSetName",hosts,0);

//Connect to it now:

    connection.connect();

//Authenticate to the database(s) if needed

    std::string errmsg;
    connection.auth("DB1Name","UserForDB1","pass1",errmsg);
    connection.auth("DB2Name","UserForDB2","pass2",errmsg);

      

Now you can use insert, update, etc. the same as with DBClientConnection

. For a quick fix, you can replace references to DBClientConnection

on DBClientBase

(which is the parent for both DBClientConnection

and DBClientReplicaSet

)

Last error: if you are using getLastError (), you must use it with a database name like this:

connection.getLastError(std::string("DBName"));

      

otherwise, it will always return "command failed: must be logged in" as described in this JIRA ticket .

Set read parameters for each request

You have two ways to do this:

SlaveOK option

It allows your read requests to be redirected to secondary servers.

This happens in the request parameters, which are at the end of the parameters DBClientReplicaSet.query()

. The parameters are specified in the official Mongo documentation

The one you would be looking for is one mongo::QueryOption_SlaveOk

that will allow you to read messages in secondary instances.



This is how you should call query ();

connection.query("Database.Collection",
     QUERY("_id" << id),
     n,
     m,
     BSON("SomeField" << 1),
     QueryOption_SlaveOk);

      

where n is the number of documents returned (0 if you don't want any limit), m is the number to skip (default is 0), the next field is your projection and your last request.

To use multiple query parameters, you can use bitwise or |

like this:

connection.query("Database.Collection",
     QUERY("_id" << id),
     n,
     m,
     BSON("SomeField" << 1),
     QueryOption_SlaveOk | QueryOption_NoCursorTimeout | QueryOption_Exhaust);

      

Query :: readPref parameter

The Query object has a readPref method that sets the read preference for a custom query. It must be called for every request.

You can pass different arguments for more control. They are listed here .

So what should you do (I haven't tested this reason, I can't now, but it should work fine)

/* you should pass an array for the tags. Not sure if this is required.
Anyway, let create an empty array using the builder. */

BSONArrayBuilder bab;

/* if any, add your tags here */

connection.query("Database.Collection",
     QUERY("_id" << id).readPref(ReadPreference_SecondaryPreferred, bab.arr()),
     n,
     m,
     BSON("SomeField" << 1),
     QueryOption_NoCursorTimeout | QueryOption_Exhaust);

      

Note : if any readPref option is used, it must override the slaveOk parameter.

Hope it helped.

+3


source


For details on the format of the connection string, see the documentation.

(links to the code below are for files 2.2.3)

To use a connection string with a C ++ driver, you must use the class ConnectionString

. First, you call a static method ConnectionString::parse

with a connection string to get ConnectionString

. Then you call ConnectionString::connect

to get a DBClientBase object , which you can then use to send requests.



As far as read preference is concerned, at the moment I see no way to set a read preference in the connection string for the C ++ driver, which would preclude a per-connection setting.

However, the implementation DBClientBase

returned by the call ConnectionString::parse

with the string that identifies the replica set will return you a DBClientReplicaSet instance . This class evaluates $readPreference

in requests, so you can adjust the read preference for each request.

+2


source


Since current C ++ drivers still do not accept the standard mongodb URIs, I opened a ticket: https://jira.mongodb.org/browse/CXX-2 Please vote for it to help fix this.

+1


source


It seems that you can set a preprint before sending a read request using the "readPref" method of your Query object. I haven't found a way to set the Preference on mongo collection parameter yet.

0


source







All Articles