Skip to content

Commit 95492f4

Browse files
authored
DOCSP-38960: Add Atlas code to landing page (#10)
* DOCSP-38960: Add Atlas code to landing page * wording * source constant
1 parent adf151c commit 95492f4

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <mongoc/mongoc.h>
2+
3+
int main(void) {
4+
mongoc_client_t *client = NULL;
5+
bson_error_t error = {0};
6+
mongoc_database_t *database = NULL;
7+
bson_t *command = NULL;
8+
bson_t reply = BSON_INITIALIZER;
9+
int rc = 0;
10+
bool ok = true;
11+
12+
// Initialize the MongoDB C Driver.
13+
mongoc_init();
14+
15+
client = mongoc_client_new("<connection string>");
16+
if (!client) {
17+
fprintf(stderr, "Failed to create a MongoDB client.\n");
18+
rc = 1;
19+
goto cleanup;
20+
}
21+
22+
// Get a handle on the "admin" database.
23+
database = mongoc_client_get_database(client, "admin");
24+
if (!database) {
25+
fprintf(stderr, "Failed to get a MongoDB database handle.\n");
26+
rc = 1;
27+
goto cleanup;
28+
}
29+
30+
// Ping the database.
31+
command = BCON_NEW("ping", BCON_INT32(1));
32+
ok = mongoc_database_command_simple(
33+
database, command, NULL, &reply, &error
34+
);
35+
if (!ok) {
36+
fprintf(stderr, "error: %s\n", error.message);
37+
rc = 1;
38+
goto cleanup;
39+
}
40+
bson_destroy(&reply);
41+
42+
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
43+
44+
// Perform cleanup.
45+
cleanup:
46+
bson_destroy(command);
47+
mongoc_database_destroy(database);
48+
mongoc_client_destroy(client);
49+
mongoc_cleanup();
50+
51+
return rc;
52+
}

source/includes/c-connection.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <mongoc/mongoc.h>
2+
3+
int main(void) {
4+
mongoc_client_t *client = NULL;
5+
bson_error_t error = {0};
6+
mongoc_server_api_t *api = NULL;
7+
mongoc_database_t *database = NULL;
8+
bson_t *command = NULL;
9+
bson_t reply = BSON_INITIALIZER;
10+
int rc = 0;
11+
bool ok = true;
12+
13+
// Initialize the MongoDB C Driver.
14+
mongoc_init();
15+
16+
client = mongoc_client_new("<connection string>");
17+
if (!client) {
18+
fprintf(stderr, "Failed to create a MongoDB client.\n");
19+
rc = 1;
20+
goto cleanup;
21+
}
22+
23+
// Set the version of the Stable API on the client.
24+
api = mongoc_server_api_new(MONGOC_SERVER_API_V1);
25+
if (!api) {
26+
fprintf(stderr, "Failed to create a MongoDB server API.\n");
27+
rc = 1;
28+
goto cleanup;
29+
}
30+
31+
ok = mongoc_client_set_server_api(client, api, &error);
32+
if (!ok) {
33+
fprintf(stderr, "error: %s\n", error.message);
34+
rc = 1;
35+
goto cleanup;
36+
}
37+
38+
// Get a handle on the "admin" database.
39+
database = mongoc_client_get_database(client, "admin");
40+
if (!database) {
41+
fprintf(stderr, "Failed to get a MongoDB database handle.\n");
42+
rc = 1;
43+
goto cleanup;
44+
}
45+
46+
// Ping the database.
47+
command = BCON_NEW("ping", BCON_INT32(1));
48+
ok = mongoc_database_command_simple(
49+
database, command, NULL, &reply, &error
50+
);
51+
if (!ok) {
52+
fprintf(stderr, "error: %s\n", error.message);
53+
rc = 1;
54+
goto cleanup;
55+
}
56+
bson_destroy(&reply);
57+
58+
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
59+
60+
// Perform cleanup.
61+
cleanup:
62+
bson_destroy(command);
63+
mongoc_database_destroy(database);
64+
mongoc_server_api_destroy(api);
65+
mongoc_client_destroy(client);
66+
mongoc_cleanup();
67+
68+
return rc;
69+
}

source/index.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,39 @@ following our tutorial.
5353

5454
- `Additional BSON Examples <https://github.com/mongodb/mongo-c-driver/tree/master/src/libbson/examples>`__
5555

56+
Connect to MongoDB Atlas
57+
------------------------
58+
59+
You can use the following connection snippet to test your connection to your
60+
MongoDB deployment on Atlas:
61+
62+
.. literalinclude:: /includes/c-connection.c
63+
:language: c
64+
65+
This connection snippet uses the Stable API feature. You can access this feature
66+
when connecting to MongoDB Server v5.0 and later and using the C driver v1.18 and later.
67+
68+
When you use this feature, you can update your driver or server without
69+
worrying about backward compatibility issues with any commands covered by the
70+
Stable API.
71+
72+
.. note::
73+
74+
Starting from February 2022, the **Versioned API** is known as the **Stable API**.
75+
All concepts and features remain the same with this naming change.
76+
77+
.. _connect-atlas-no-stable-api-c-driver:
78+
79+
Connect to MongoDB Atlas Without the Stable API
80+
-----------------------------------------------
81+
82+
If you are using a version of MongoDB or the driver that lacks support for the
83+
Stable API, you can use the following code snippet to test your connection
84+
to your MongoDB deployment on Atlas:
85+
86+
.. literalinclude:: /includes/c-connection-no-stable-api.c
87+
:language: c
88+
5689
Compatibility
5790
-------------
5891

0 commit comments

Comments
 (0)