Skip to content

Commit c263c8a

Browse files
author
Chris Cho
authored
DOCSP-28246 stable api updates (#857)
* DOCSP-28246: Stable API updates
1 parent d4bb399 commit c263c8a

File tree

6 files changed

+203
-68
lines changed

6 files changed

+203
-68
lines changed

source/c.txt

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,75 +38,37 @@ Installation
3838
See `Installing the MongoDB C Driver (libmongoc) and BSON library (libbson)
3939
<https://mongoc.org/libmongoc/current/installing.html>`__.
4040

41-
4241
.. _connect-atlas-c-driver:
4342

4443
Connect to MongoDB Atlas
4544
------------------------
4645

47-
.. include:: /includes/atlas-connect-blurb.rst
48-
49-
.. code-block:: c
50-
51-
#include <mongoc/mongoc.h>
52-
53-
int
54-
main (int argc, char *argv[])
55-
{
56-
mongoc_database_t *database;
57-
mongoc_client_t *client;
58-
59-
mongoc_init ();
60-
61-
// Replace the uri string with your MongoDB deployment's connection string.
62-
client = mongoc_client_new(
63-
"mongodb+srv://<username>:<password>@<cluster-url>/test?retryWrites=true&w=majority"
64-
);
65-
database = mongoc_client_get_database (client, "test");
66-
67-
mongoc_database_destroy (database);
68-
mongoc_client_destroy (client);
69-
70-
mongoc_cleanup ();
71-
72-
return 0;
73-
}
74-
75-
.. include:: /includes/serverless-compatibility.rst
46+
You can use the following connection snippet to test your connection to your
47+
MongoDB deployment on Atlas:
7648

77-
See `Advanced Connections <https://mongoc.org/libmongoc/current/advanced-connections.html>`__
78-
for more ways to connect.
49+
.. literalinclude:: /includes/connection-snippets/scram/c-connection.c
50+
:language: c
7951

80-
{+stable-api+}
81-
--------------
52+
This connection snippet uses the {+stable-api+} feature which you can use when
53+
connecting to MongoDB Server v5.0 and later and the C driver v1.18 and later.
8254

83-
You can use the {+stable-api+} feature starting with MongoDB Server version
84-
5.0 and C Driver version 1.18. When you use the {+stable-api+} feature,
85-
you can update your driver or server without worrying about backward
86-
compatibility issues with any commands covered by the {+stable-api+}.
55+
When you use this feature, you can update your driver or server without
56+
worrying about backward compatibility issues with any commands covered by the
57+
{+stable-api+}.
8758

8859
.. include:: /includes/stable-api-notice.rst
8960

90-
To use this feature, construct a MongoDB client instance, specifying a version
91-
of the {+stable-api+}:
92-
93-
.. code-block:: c
94-
95-
// Replace <connection string> with your MongoDB deployment's connection string.
96-
const char *uri_string = "<connection string>";
97-
mongoc_uri_t *uri;
98-
mongoc_client_t *client;
99-
mongoc_server_api_t *api;
100-
bson_error_t error;
61+
.. _connect-atlas-no-stable-api-c-driver:
10162

102-
mongoc_init ();
63+
Connect to MongoDB Atlas Without the Stable API
64+
-----------------------------------------------
10365

104-
uri = mongoc_uri_new_with_error (uri_string, &error);
105-
client = mongoc_client_new_from_uri (uri);
66+
If you are using a version of MongoDB or the driver that lacks support for the
67+
{+stable-api+}, you can use the following code snippet to test your connection
68+
to your MongoDB deployment on Atlas:
10669

107-
// Set the version of the {+stable-api+} on the client.
108-
api = mongoc_server_api_new (MONGOC_SERVER_API_V1);
109-
mongoc_client_set_server_api (client, api, &error);
70+
.. literalinclude:: /includes/connection-snippets/scram/c-connection-no-stable-api.c
71+
:language: c
11072

11173
Connect to a MongoDB Server on Your Local Machine
11274
-------------------------------------------------
@@ -117,6 +79,11 @@ To test whether you can connect to your server, replace the connection
11779
string in the :ref:`Connect to MongoDB Atlas <connect-atlas-c-driver>` code
11880
example and run it.
11981

82+
See `Advanced Connections <https://mongoc.org/libmongoc/current/advanced-connections.html>`__
83+
for more ways to connect.
84+
85+
.. include:: /includes/serverless-compatibility.rst
86+
12087
Compatibility
12188
-------------
12289

source/cxx.txt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,32 @@ See `Installing the mongocxx driver <https://mongocxx.org/mongocxx-v3/installati
4242
Connect to MongoDB Atlas
4343
------------------------
4444

45-
.. include:: /includes/atlas-connect-blurb.rst
45+
You can use the following connection snippet to test your connection to your
46+
MongoDB deployment on Atlas:
4647

47-
.. code-block:: cpp
48+
.. literalinclude:: /includes/connection-snippets/scram/cxx-connection.cpp
49+
:language: cpp
4850

49-
#include <mongocxx/client.hpp>
50-
#include <mongocxx/instance.hpp>
51+
This connection snippet uses the {+stable-api+} feature which you can use when
52+
connecting to MongoDB Server v5.0 and later and the C++ driver v3.7 and later.
5153

52-
//...
54+
When you use this feature, you can update your driver or server without
55+
worrying about backward compatibility issues with any commands covered by the
56+
{+stable-api+}.
5357

54-
mongocxx::instance inst{}; // This should be done only once.
55-
mongocxx::client conn{
56-
mongocxx::uri{
57-
"mongodb+srv://<username>:<password>@<cluster-url>/test?retryWrites=true&w=majority"
58-
}
59-
};
60-
mongocxx::database db = conn["test"];
58+
.. include:: /includes/stable-api-notice.rst
6159

62-
.. include:: /includes/serverless-compatibility.rst
60+
.. _connect-atlas-no-stable-api-cxx-driver:
61+
62+
Connect to MongoDB Atlas Without the Stable API
63+
-----------------------------------------------
64+
65+
If you are using a version of MongoDB or the driver that lacks support for the
66+
{+stable-api+}, you can use the following code snippet to test your connection
67+
to your MongoDB deployment on Atlas:
68+
69+
.. literalinclude:: /includes/connection-snippets/scram/cxx-connection-no-stable-api.cpp
70+
:language: cpp
6371

6472
Connect to a MongoDB Server on Your Local Machine
6573
-------------------------------------------------
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <mongoc/mongoc.h>
2+
3+
int main (int argc, char **argv)
4+
{
5+
mongoc_client_t *client = NULL;
6+
bson_error_t error = {0};
7+
mongoc_database_t *database = NULL;
8+
bson_t *command = NULL, reply;
9+
10+
11+
// Initialize the MongoDB C Driver.
12+
mongoc_init ();
13+
14+
// Replace the <connection string> with your MongoDB deployment's connection string.
15+
client = mongoc_client_new("<connection string>");
16+
17+
// Get a handle on the "admin" database.
18+
database = mongoc_client_get_database (client, "admin");
19+
20+
// Ping the database.
21+
command = BCON_NEW("ping", BCON_INT32(1));
22+
if (mongoc_database_command_simple(database, command, NULL, &reply, &error))
23+
{
24+
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
25+
}
26+
else
27+
{
28+
// Error condition.
29+
printf("Error: %s\n", error.message);
30+
return 0;
31+
}
32+
33+
34+
// Perform Cleanup.
35+
bson_destroy (&reply);
36+
bson_destroy (command);
37+
mongoc_database_destroy (database);
38+
mongoc_client_destroy (client);
39+
mongoc_cleanup ();
40+
41+
return 0;
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <mongoc/mongoc.h>
2+
3+
int main (int argc, char **argv)
4+
{
5+
mongoc_client_t *client = NULL;
6+
bson_error_t error = {0};
7+
mongoc_server_api_t *api = NULL;
8+
mongoc_database_t *database = NULL;
9+
bson_t *command = NULL, reply;
10+
11+
// Initialize the MongoDB C Driver.
12+
mongoc_init ();
13+
14+
// Replace the <connection string> with your MongoDB deployment's connection string.
15+
client = mongoc_client_new("<connection string>");
16+
17+
// Set the version of the Stable API on the client.
18+
api = mongoc_server_api_new (MONGOC_SERVER_API_V1);
19+
if (!mongoc_client_set_server_api (client, api, &error))
20+
{
21+
// Error condition.
22+
printf("Error: %s\n", error.message);
23+
return 0;
24+
}
25+
26+
// Get a handle on the "admin" database.
27+
database = mongoc_client_get_database (client, "admin");
28+
29+
// Ping the database.
30+
command = BCON_NEW("ping", BCON_INT32(1));
31+
if (mongoc_database_command_simple(database, command, NULL, &reply, &error))
32+
{
33+
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
34+
}
35+
else
36+
{
37+
// Error condition.
38+
printf("Error: %s\n", error.message);
39+
return 0;
40+
}
41+
42+
43+
// Perform Cleanup.
44+
bson_destroy (&reply);
45+
bson_destroy (command);
46+
mongoc_database_destroy (database);
47+
mongoc_client_destroy (client);
48+
mongoc_cleanup ();
49+
50+
return 0;
51+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bsoncxx/json.hpp>
2+
#include <mongocxx/client.hpp>
3+
#include <mongocxx/instance.hpp>
4+
5+
int main()
6+
{
7+
try
8+
{
9+
// Create an instance.
10+
mongocxx::instance inst{};
11+
12+
// Replace the connection string with your MongoDB deployment's connection string.
13+
const auto uri = mongocxx::uri{"<connection string>"};
14+
15+
// Setup the connection and get a handle on the "admin" database.
16+
mongocxx::client conn{ uri };
17+
mongocxx::database db = conn["admin"];
18+
19+
// Ping the database.
20+
const auto ping_cmd = bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("ping", 1));
21+
db.run_command(ping_cmd.view());
22+
std::cout << "Pinged your deployment. You successfully connected to MongoDB!" << std::endl;
23+
}
24+
catch (const std::exception& e)
25+
{
26+
// Handle errors.
27+
std::cout<< "Exception: " << e.what() << std::endl;
28+
}
29+
30+
return 0;
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bsoncxx/json.hpp>
2+
#include <mongocxx/client.hpp>
3+
#include <mongocxx/instance.hpp>
4+
5+
int main()
6+
{
7+
try
8+
{
9+
// Create an instance.
10+
mongocxx::instance inst{};
11+
12+
// Replace the connection string with your MongoDB deployment's connection string.
13+
const auto uri = mongocxx::uri{"<connection string>"};
14+
15+
// Set the version of the Stable API on the client.
16+
mongocxx::options::client client_options;
17+
const auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1 };
18+
client_options.server_api_opts(api);
19+
20+
// Setup the connection and get a handle on the "admin" database.
21+
mongocxx::client conn{ uri, client_options };
22+
mongocxx::database db = conn["admin"];
23+
24+
// Ping the database.
25+
const auto ping_cmd = bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("ping", 1));
26+
db.run_command(ping_cmd.view());
27+
std::cout << "Pinged your deployment. You successfully connected to MongoDB!" << std::endl;
28+
}
29+
catch (const std::exception& e)
30+
{
31+
// Handle errors.
32+
std::cout<< "Exception: " << e.what() << std::endl;
33+
}
34+
35+
return 0;
36+
}

0 commit comments

Comments
 (0)