Skip to content

Commit 4cd41f8

Browse files
author
Mohammad Hunan Chughtai
authored
(DOCSP-7885): x509 c snippet (#587)
* (DOCSP-7885): added C connection code * added count_document to c example * added collection.destroy to prevent leak on clang x509 snip * (DOCSP-7885): updated c snippet to address comments * updated c code with proper formatting using clang-format -style=file -i * update error and move variable declorations to top
1 parent 0409629 commit 4cd41f8

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// begin x509 connection
2+
#include <mongoc/mongoc.h>
3+
#include <stdio.h>
4+
5+
int main(int argc, char *argv[]) {
6+
mongoc_collection_t *collection;
7+
mongoc_client_t *client;
8+
int64_t count;
9+
bson_t filter;
10+
bson_error_t error;
11+
12+
mongoc_init();
13+
14+
client = mongoc_client_new(
15+
"mongodb+srv://<cluster-url>/"
16+
"test?authSource=$external&tlsCertificateKeyFile=/etc/certs/mongodb/"
17+
"client.pem&retryWrites=true&w=majority&authMechanism=MONGODB-X509");
18+
19+
collection = mongoc_client_get_collection(client, "testDB", "testCol");
20+
filter = BSON_INITIALIZER;
21+
count = mongoc_collection_count_documents(collection, &filter, NULL, NULL,
22+
NULL, &error);
23+
24+
if (count < 0) {
25+
fprintf(stderr, "Count failed: %s\n", error.message);
26+
} else {
27+
printf("%" PRId64 " documents counted.\n", count);
28+
}
29+
bson_destroy(&filter);
30+
mongoc_collection_destroy(collection);
31+
mongoc_client_destroy(client);
32+
mongoc_cleanup();
33+
34+
return 0;
35+
}
36+
// end x509 connection

0 commit comments

Comments
 (0)