Connect to MongoDB
On this page
Overview
This page contains code examples that show how to use the C driver to connect your application to MongoDB by specifying various settings.
Tip
Connection Options
To learn more about the connection options on this page, see the link provided in each section.
To use a connection example from this page, copy the code example into the
sample application or your own application.
Be sure to replace all placeholders in the code examples, such as
<hostname>
, with the relevant values for your MongoDB deployment.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the C driver installed.
Copy the following code and paste it into a new
.c
file.Copy a code example from this page and paste it on the specified lines in the file.
1 2 3 4 int main(void) { 5 6 mongoc_uri_t* uri = NULL; 7 mongoc_client_t *client = NULL; 8 mongoc_database_t *database = NULL; 9 bson_t *ping = NULL, reply = BSON_INITIALIZER; 10 bson_error_t error; 11 12 mongoc_init(); 13 14 // Start example code here 15 16 // End example code here 17 18 database = mongoc_client_get_database (client, "admin"); 19 20 ping = BCON_NEW ("ping", BCON_INT32 (1)); 21 22 if (!mongoc_client_command_simple (client, "admin", ping, NULL, &reply, &error)) { 23 fprintf (stderr, "%s\n", error.message); 24 goto cleanup; 25 } 26 printf ("Pinged your deployment. You successfully connected to MongoDB!\n"); 27 28 cleanup: 29 bson_destroy (&reply); 30 bson_destroy (ping); 31 mongoc_database_destroy (database); 32 mongoc_client_destroy (client); 33 mongoc_uri_destroy (uri); 34 mongoc_cleanup (); 35 }
Connection
The following sections describe how to connect to different targets, such as a local instance of MongoDB or a cloud-hosted instance on Atlas.
Local Deployment
The following code shows the connection string to connect to a local instance of MongoDB:
client = mongoc_client_new ("mongodb://localhost:27017");
To learn more about connecting to local deployments, see Local Deployments in the Choose a Connection Target guide.
Atlas
The following code shows the connection string to connect to a deployment hosted on Atlas:
client = mongoc_client_new ("mongodb+srv://<db_username>:<db_password>@<hostname>/?<options>");
To learn more about connecting to Atlas, see Atlas in the Choose a Connection Target guide.
Replica Set
The following code shows the connection string to connect to a replica set:
client = mongoc_client_new ("mongodb+srv://<replica-set-member>/?replicaSet=<replica_set_name>");
To learn more about connecting to replica sets, see Replica Sets in the Choose a Connection Target guide.
Transport Layer Security (TLS)
The following sections describe how to connect to MongoDB while enabling the TLS protocol.
To learn more about using TLS with the C driver, see Configure Transport Layer Security (TLS).
Enable TLS
The following tabs demonstrate how to enable TLS on a connection:
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?tls=true");
uri = mongoc_uri_new ("mongodb://localhost:27017"); mongoc_uri_set_option_as_bool (uri, MONGOC_URI_TLS, true); client = mongoc_client_new_from_uri (uri);
To learn more about enabling TLS, see Enable TLS in the TLS configuration guide.
Disable Hostname Verification
The following tabs demonstrate how to disable hostname verification when connecting by using TLS:
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?tls=true&tlsAllowInvalidHostnames=true");
uri = mongoc_uri_new ("mongodb://localhost:27017"); mongoc_uri_set_option_as_bool (uri, MONGOC_URI_TLS, true); mongoc_uri_set_option_as_bool (uri, MONGOC_URI_TLSALLOWINVALIDHOSTNAMES, true); client = mongoc_client_new_from_uri (uri);
To learn more about disabling hostname verification, see Server Certificate Verification in the TLS configuration guide.
Network Compression
The following sections describe how to connect to MongoDB while specifying network compression algorithms.
Compression Algorithms
The following tabs demonstrate how to specify all available compressors while connecting to MongoDB:
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?compressors=snappy,zlib,zstd");
uri = mongoc_uri_new ("mongodb://localhost:27017"); mongoc_uri_set_compressors (uri, "snappy,zlib,zstd"); client = mongoc_client_new_from_uri (uri);
zlib Compression Level
The following tabs demonstrate how to specify a compression level for
the zlib
compressor:
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?compressors=zlib&zlibCompressionLevel=<zlib_compression_level");
uri = mongoc_uri_new ("mongodb://localhost:27017"); mongoc_uri_set_option_as_int32 (uri, MONGOC_URI_ZLIBCOMPRESSIONLEVEL, <zlib-compression-level>); client = mongoc_client_new_from_uri (uri);
Server Selection
The following code shows a connection string that specifies a server selection function:
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?server_selector=<selector_function>");
Stable API
The following code shows how to specify Stable API settings within a
mongoc_client_t
instance:
client = mongoc_client_new ("mongodb+srv://<db_username>:<db_password>@<hostname>/?<options>"); // Set the version of the Stable API on the client mongoc_server_api_t *api = mongoc_server_api_new(MONGOC_SERVER_API_V1); mongoc_client_set_server_api(client, api, &error); // Do database work here mongoc_server_api_destroy (api);
To learn more about the Stable API, see Stable API.