mongoc_uri_t - mongoc_uri_t

NAME  SYNOPSIS  DESCRIPTION  FORMAT  REPLICA SET EXAMPLE  SRV EXAMPLE  IPV4 AND IPV6  CONNECTION OPTIONS  AUTHENTICATION OPTIONS  Mechanism Properties  TLS OPTIONS  DEPRECATED SSL OPTIONS  SERVER DISCOVERY, MONITORING, AND SELECTION OPTIONS  CONNECTION POOL OPTIONS  WRITE CONCERN OPTIONS  READ CONCERN OPTIONS  READ PREFERENCE OPTIONS  LEGACY OPTIONS  AUTHOR  COPYRIGHT 

NAME

mongoc_uri_t − mongoc_uri_t

SYNOPSIS

typedef struct _mongoc_uri_t mongoc_uri_t;

DESCRIPTION

mongoc_uri_t provides an abstraction on top of the MongoDB connection URI format. It provides standardized parsing as well as convenience methods for extracting useful information such as replica hosts or authorization information.

See Connection String URI Reference on the MongoDB website for more information.

FORMAT

mongodb[+srv]://                             <1>
   [username:password@]                      <2>
   host1                                     <3>
   [:port1]                                  <4>
   [,host2[:port2],...[,hostN[:portN]]]      <5>
   [/[database]                              <6>
   [?options]]                               <7>

1.

"mongodb" is the specifier of the MongoDB protocol. Use "mongodb+srv" with a single service name in place of "host1" to specify the initial list of servers with an SRV record.

2.

An optional username and password.

3.

The only required part of the uri. This specifies either a hostname, IPv4 address, IPv6 address enclosed in "[" and "]", or UNIX domain socket.

4.

An optional port number. Defaults to :27017.

5.

Extra optional hosts and ports. You would specify multiple hosts, for example, for connections to replica sets.

6.

The name of the database to authenticate if the connection string includes authentication credentials. If /database is not specified and the connection string includes credentials, defaults to the 'admin' database.

7.

Connection specific options.

NOTE:

Option names are case−insensitive. Do not repeat the same option (e.g. "mongodb://localhost/db?opt=value1&OPT=value2") since this may have unexpected results.

The MongoDB C Driver exposes constants for each supported connection option. These constants make it easier to discover connection options, but their string values can be used as well.

For example, the following calls are equal.

uri = mongoc_uri_new ("mongodb://localhost/?" MONGOC_URI_APPNAME "=applicationName");
uri = mongoc_uri_new ("mongodb://localhost/?appname=applicationName");
uri = mongoc_uri_new ("mongodb://localhost/?appName=applicationName");

REPLICA SET EXAMPLE

To describe a connection to a replica set named 'test' with the following mongod hosts:

db1.example.com on port 27017

db2.example.com on port 2500

You would use a connection string that resembles the following.

mongodb://db1.example.com,db2.example.com:2500/?replicaSet=test

SRV EXAMPLE

If you have configured an SRV record with a name like "_mongodb._tcp.server.example.com" whose records are a list of one or more MongoDB server hostnames, use a connection string like this:

uri = mongoc_uri_new ("mongodb+srv://server.example.com/?replicaSet=rs&appName=applicationName");

The driver prefixes the service name with "_mongodb._tcp.", then performs a DNS SRV query to resolve the service name to one or more hostnames. If this query succeeds, the driver performs a DNS TXT query on the service name (without the "_mongodb._tcp" prefix) for additional URI options configured as TXT records.

On Unix, the MongoDB C Driver relies on libresolv to look up SRV and TXT records. If libresolv is unavailable, then using a "mongodb+srv" URI will cause an error. If your libresolv lacks res_nsearch then the driver will fall back to res_search, which is not thread−safe.

IPV4 AND IPV6

If connecting to a hostname that has both IPv4 and IPv6 DNS records, the behavior follows RFC−6555. A connection to the IPv6 address is attempted first. If IPv6 fails, then a connection is attempted to the IPv4 address. If the connection attempt to IPv6 does not complete within 250ms, then IPv4 is tried in parallel. Whichever succeeds connection first cancels the other. The successful DNS result is cached for 10 minutes.

As a consequence, attempts to connect to a mongod only listening on IPv4 may be delayed if there are both A (IPv4) and AAAA (IPv6) DNS records associated with the host.

To avoid a delay, configure hostnames to match the MongoDB configuration. That is, only create an A record if the mongod is only listening on IPv4.

CONNECTION OPTIONS

Setting any of the *timeoutMS options above to 0 will be interpreted as "use the default value".

AUTHENTICATION OPTIONS

Mechanism Properties

TLS OPTIONS

See Configuring TLS for details about these options and about building libmongoc with TLS support.

DEPRECATED SSL OPTIONS

The following options have been deprecated and may be removed from future releases of libmongoc.

SERVER DISCOVERY, MONITORING, AND SELECTION OPTIONS

Clients in a mongoc_client_pool_t share a topology scanner that runs on a background thread. The thread wakes every heartbeatFrequencyMS (default 10 seconds) to scan all MongoDB servers in parallel. Whenever an application operation requires a server that is not known−−for example, if there is no known primary and your application attempts an insert−−the thread rescans all servers every half−second. In this situation the pooled client waits up to serverSelectionTimeoutMS (default 30 seconds) for the thread to find a server suitable for the operation, then returns an error with domain MONGOC_ERROR_SERVER_SELECTION.

Technically, the total time an operation may wait while a pooled client scans the topology is controlled both by serverSelectionTimeoutMS and connectTimeoutMS. The longest wait occurs if the last scan begins just at the end of the selection timeout, and a slow or down server requires the full connection timeout before the client gives up.

A non−pooled client is single−threaded. Every heartbeatFrequencyMS, it blocks the next application operation while it does a parallel scan. This scan takes as long as needed to check the slowest server: roughly connectTimeoutMS. Therefore the default heartbeatFrequencyMS for single−threaded clients is greater than for pooled clients: 60 seconds.

By default, single−threaded (non−pooled) clients scan only once when an operation requires a server that is not known. If you attempt an insert and there is no known primary, the client checks all servers once trying to find it, then succeeds or returns an error with domain MONGOC_ERROR_SERVER_SELECTION. But if you set serverSelectionTryOnce to "false", the single−threaded client loops, checking all servers every half−second, until serverSelectionTimeoutMS.

The total time an operation may wait for a single−threaded client to scan the topology is determined by connectTimeoutMS in the try−once case, or serverSelectionTimeoutMS and connectTimeoutMS if serverSelectionTryOnce is set "false".

Setting any of the *TimeoutMS options above to 0 will be interpreted as "use the default value".

CONNECTION POOL OPTIONS

These options govern the behavior of a mongoc_client_pool_t. They are ignored by a non−pooled mongoc_client_t.

WRITE CONCERN OPTIONS

READ CONCERN OPTIONS

READ PREFERENCE OPTIONS

When connected to a replica set, the driver chooses which member to query using the read preference:

1.

Choose members whose type matches "readPreference".

2.

From these, if there are any tags sets configured, choose members matching the first tag set. If there are none, fall back to the next tag set and so on, until some members are chosen or the tag sets are exhausted.

3.

From the chosen servers, distribute queries randomly among the server with the fastest round−trip times. These include the server with the fastest time and any whose round−trip time is no more than "localThresholdMS" slower.

NOTE:

When connecting to more than one mongos, libmongoc's localThresholdMS applies only to the selection of mongos servers. The threshold for selecting among replica set members in shards is controlled by the mongos's localThreshold command line option.

LEGACY OPTIONS

For historical reasons, the following options are available. They should however not be used.

AUTHOR

MongoDB, Inc

COPYRIGHT

2017-present, MongoDB, Inc


Updated 2024-01-29 - jenkler.se | uex.se