MongoDB C++ Driver  legacy-1.1.2
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
mongo::ConnectionString Class Reference

ConnectionString can parse MongoDB URIs with the following format: More...

#include <dbclientinterface.h>


class  ConnectionHook

Public Types

enum  ConnectionType {

Public Member Functions

 ConnectionString (const HostAndPort &server)
 ConnectionString (ConnectionType type, const std::string &s, const std::string &setName="")
 ConnectionString (const std::string &s, ConnectionType favoredMultipleType)
bool isValid () const
std::string toString () const
DBClientBaseconnect (std::string &errmsg, double socketTimeout=0) const
std::string getSetName () const
const std::vector< HostAndPort > & getServers () const
ConnectionType type () const
const std::string & getUser () const
const std::string & getPassword () const
const BSONObjgetOptions () const
const std::string & getDatabase () const
bool sameLogicalEndpoint (const ConnectionString &other) const
 This returns true if this and other point to the same logical entity. More...
bool operator< (const ConnectionString &other) const

Static Public Member Functions

static ConnectionString MONGO_CLIENT_FUNC parse (const std::string &address, std::string &errmsg)
static ConnectionString MONGO_CLIENT_FUNC parseDeprecated (const std::string &address, std::string &errmsg)
static std::string MONGO_CLIENT_FUNC typeToString (ConnectionType type)
static void setConnectionHook (ConnectionHook *hook)
static ConnectionHookgetConnectionHook ()
static ConnectionString mock (const HostAndPort &server)

Detailed Description

ConnectionString can parse MongoDB URIs with the following format:


For a complete list of URI string options, see


A replica set with three members (one running on default port 27017): string uri = mongodb://localhost,localhost:27018,localhost:27019

Authenticated connection to db 'bedrock' with user 'barney' and pwd 'rubble': string url = mongodb://barney:rubble/bedrock

Use parse() to parse the url, then validate and connect: string errmsg; ConnectionString cs = ConnectionString::parse( url, errmsg ); if ( ! cs.isValid() ) throw "bad connection string: " + errmsg; DBClientBase * conn = cs.connect( errmsg );


The 'rs_name/host1:port,host2:port' format has been deprecated, and parse() will no longer recognize this as a valid URI. To use the deprecated format, use parseDeprecated() instead.

Member Function Documentation

bool mongo::ConnectionString::sameLogicalEndpoint ( const ConnectionString other) const

This returns true if this and other point to the same logical entity.

For single nodes, thats the same address. For replica sets, thats just the same replica set name. For pair (deprecated) or sync cluster connections, that's the same hosts in any ordering.

The documentation for this class was generated from the following files: