Provided by: libmongoc-doc_1.3.1-1_all bug

NAME

       Advanced_Connections - None

CONNECTING TO A REPLICA SET

       Connecting to a replica set is much like connecting to a standalone MongoDB server. Simply
       specify the replica set name using the ?replicaSet=myreplset URI option.

       #include <bson.h>
       #include <mongoc.h>

       int
       main (int   argc,
             char *argv[])
       {
           mongoc_client_t *client;

           mongoc_init ();

           /* Create our MongoDB Client */
           client = mongoc_client_new ("mongodb://host01:27017,host02:27017,host03:27017/?replicaSet=myreplset");

           /* Do some work */
           /* TODO */

           /* Clean up */
           mongoc_client_destroy (client);
           mongoc_cleanup ();

           return 0;
       }

       NOTE
              Multiple hostnames can be specified in the MongoDB connection string  URI,  with  a
              comma separating hosts in the seed list.

              It  is  recommended  to  use a seed list of members of the replica set to allow the
              driver to connect to any node.

CONNECTING TO A SHARDED CLUSTER

       To connect to a sharded cluster , specify the mongos nodes the client should  connect  to.
       The C Driver will automatically detect that it has connected to a mongos sharding server.

       If  more  than  one hostname is specified, a seed list will be created to attempt failover
       between the mongos instances.

       NOTE
              Specifying the replicaSet parameter when connecting to a mongos sharding server  is
              invalid.

       #include <bson.h>
       #include <mongoc.h>

       int
       main (int   argc,
             char *argv[])
       {
           mongoc_client_t *client;

           mongoc_init ();

           /* Create our MongoDB Client */
           client = mongoc_client_new ("mongodb://myshard01:27017/");

           /* Do something with client ... */

           /* Free the client */
           mongoc_client_destroy (client);

           mongoc_cleanup ();

           return 0;
       }

CONNECTING TO AN IPV6 ADDRESS

       The  MongoDB  C Driver will automatically resolve IPv6 addresses from host names. However,
       to specify an IPv6 address directly, wrap the address in [] \&.

       mongoc_uri_t *uri = mongoc_uri_new ("mongodb://[::1]:27017");

CONNECTING TO A UNIX DOMAIN SOCKET

       On UNIX‐like systems, the C Driver can connect directly to a MongoDB server using  a  UNIX
       domain socket. Simply pass the path to the socket, which must be suffixed with .sock \&.

       mongoc_uri_t *uri = mongoc_uri_new ("mongodb:///tmp/mysocket.sock");

       NOTE
              Connecting directly to a UNIX domain socket is not a common practice.

ADDITIONAL CONNECTION OPTIONS

       A variety of connection options for the MongoDB URI can be found here \&.

COLOPHON

       This    page   is   part   of   MongoDB   C   Driver.    Please   report   any   bugs   at
       https://jira.mongodb.org/browse/CDRIVER.