connect
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset
Available tabs: other tabs: connectionstring, mongocurioptions -->
Connect to MongoDB
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](https://www.mongodb.com/docs/languages/c/c-driver/current/connect.md#std-label-c-connect-sample 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
.cfile. -
Copy a code example from this page and paste it on the specified lines in the file.
#include <mongoc/mongoc.h>
#include <bson/bson.h>
int main(void) {
mongoc_uri_t* uri = NULL;
mongoc_client_t *client = NULL;
mongoc_database_t *database = NULL;
bson_t *ping = NULL, reply = BSON_INITIALIZER;
bson_error_t error;
mongoc_init();
// Start example code here
// End example code here
if (!client) {
fprintf(stderr, "client initialization failure\n");
goto cleanup;
}
database = mongoc_client_get_database(client, "admin");
ping = BCON_NEW("ping", BCON_INT32(1));
if (!mongoc_client_command_simple(client, "admin", ping, NULL, &reply, &error)) {
fprintf(stderr, "%s\n", error.message);
goto cleanup;
}
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
cleanup:
bson_destroy(&reply);
bson_destroy(ping);
mongoc_database_destroy(database);
mongoc_client_destroy(client);
mongoc_uri_destroy(uri);
mongoc_cleanup();
}
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](https://www.mongodb.com/docs/languages/c/c-driver/current/connect/connection-targets.md#std-label-c-connect-local 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](https://www.mongodb.com/docs/languages/c/c-driver/current/connect/connection-targets.md#std-label-c-connect-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](https://www.mongodb.com/docs/languages/c/c-driver/current/connect/connection-targets.md#std-label-c-connect-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).](https://www.mongodb.com/docs/languages/c/c-driver/current/security/tls.md#std-label-c-tls
Enable TLS
The following tabs demonstrate how to enable TLS on a connection:
Connection String
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?tls=true");
To learn more about enabling TLS, see [Enable TLS](https://www.mongodb.com/docs/languages/c/c-driver/current/security/tls.md#std-label-c-enable-tls in the TLS configuration guide.
Disable Hostname Verification
The following tabs demonstrate how to disable hostname verification when connecting by using TLS:
Connection String
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?tls=true&tlsAllowInvalidHostnames=true");
To learn more about disabling hostname verification, see [Server Certificate Verification](https://www.mongodb.com/docs/languages/c/c-driver/current/security/tls.md#std-label-c-certificate-revocation 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:
Connection String
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?compressors=snappy,zlib,zstd");
zlib Compression Level
The following tabs demonstrate how to specify a compression level for the zlib compressor:
Connection String
client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?compressors=zlib&zlibCompressionLevel=<zlib_compression_level");
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>");
To learn more about customizing server selection, see the [Configure Read and Write Operations](https://www.mongodb.com/docs/languages/c/c-driver/current/databases-collections.md#std-label-c-config-read-write section of the Databases and Collections guide.
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.](https://www.mongodb.com/docs/languages/c/c-driver/current/connect/connection-options/stable-api.md#std-label-c-stable-api
