Skip to content

Commit 2d27a11

Browse files
CDRIVER-4556 Remove use of collStats (#1249)
* sync specification tests to commit 6632b7606e74c6e83ee2d80b7571f55485c7ff33 * remove runCommand Example 2 The example removed from DRIVERS-448. * suggest $collStats in mongoc_collection_stats * move example in mongoc_collection_command_simple to example source * add missing declaration of `str` * use `ping` instead of `collStats` command * move Executing Commands code to example * replace `collStats` with `ping` * use `mongoc_client_command_simple` To simplify the example. There is no need to run the command with the mongoc_collection_t handle. * remove note about returning cursors `mongoc_collection_command`, `mongoc_database_command` and `mongoc_client_command` return a cursor. They are documented as `superseded` by other helpers. * skip test using collStats on servers > 6.0 * remove redundant captions Co-authored-by: Ezra Chung <[email protected]> --------- Co-authored-by: Ezra Chung <[email protected]>
1 parent 25f99ee commit 2d27a11

File tree

9 files changed

+134
-102
lines changed

9 files changed

+134
-102
lines changed

src/libmongoc/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ if (ENABLE_EXAMPLES)
11131113
mongoc_add_example (mongoc-dump ${PROJECT_SOURCE_DIR}/examples/mongoc-dump.c)
11141114
mongoc_add_example (mongoc-ping ${PROJECT_SOURCE_DIR}/examples/mongoc-ping.c)
11151115
mongoc_add_example (mongoc-tail ${PROJECT_SOURCE_DIR}/examples/mongoc-tail.c)
1116+
mongoc_add_example (example-collection-command ${PROJECT_SOURCE_DIR}/examples/example-collection-command.c)
11161117

11171118
# examples/aggregation/
11181119
mongoc_add_example (aggregation1 ${PROJECT_SOURCE_DIR}/examples/aggregation/aggregation1.c)
@@ -1142,6 +1143,9 @@ if (ENABLE_EXAMPLES)
11421143
mongoc_add_example (client-side-encryption-auto-decryption ${PROJECT_SOURCE_DIR}/examples/client-side-encryption-auto-decryption.c ${PROJECT_SOURCE_DIR}/examples/client-side-encryption-helpers.c)
11431144
mongoc_add_example (client-side-encryption-doc-snippets ${PROJECT_SOURCE_DIR}/examples/client-side-encryption-doc-snippets.c)
11441145
endif ()
1146+
1147+
# examples/tutorial
1148+
mongoc_add_example (executing ${PROJECT_SOURCE_DIR}/examples/tutorial/executing.c)
11451149
endif ()
11461150

11471151
file (COPY ${PROJECT_SOURCE_DIR}/tests/binary DESTINATION ${PROJECT_BINARY_DIR}/tests)

src/libmongoc/doc/mongoc_collection_command_simple.rst

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,8 @@ This function does not check the server response for a write concern error or wr
4848
Example
4949
-------
5050

51-
The following is an example of executing the collection stats command.
52-
53-
.. code-block:: c
54-
55-
#include <bson/bson.h>
56-
#include <mongoc/mongoc.h>
57-
#include <stdio.h>
58-
59-
static void
60-
print_collection_stats (mongoc_collection_t *collection)
61-
{
62-
bson_error_t error;
63-
const char *name;
64-
bson_t *cmd;
65-
bson_t reply;
66-
67-
name = mongoc_collection_get_name (collection);
68-
cmd = BCON_NEW ("collStats", BCON_UTF8 (name));
69-
70-
if (mongoc_collection_command_simple (
71-
collection, cmd, NULL, &reply, &error)) {
72-
str = bson_as_canonical_extended_json (&reply, NULL);
73-
printf ("%s\n", str);
74-
bson_free (str);
75-
} else {
76-
fprintf (stderr, "%s\n", error.message);
77-
}
78-
79-
bson_destroy (&reply);
80-
bson_destroy (cmd);
81-
}
51+
The following is an example of executing the ``ping`` command.
8252

53+
.. literalinclude:: ../examples/example-collection-command.c
54+
:start-after: BEGIN:mongoc_collection_command
55+
:end-before: END:mongoc_collection_command

src/libmongoc/doc/mongoc_collection_stats.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Synopsis
1717
Deprecated
1818
----------
1919

20-
This helper function is deprecated and should not be used in new code. Run the `collStats <https://docs.mongodb.com/manual/reference/command/collStats/>`_ command directly with :symbol:`mongoc_client_read_command_with_opts()` instead.
20+
This helper function is deprecated and should not be used in new code. Use the `$collStats aggregation pipeline stage <https://www.mongodb.com/docs/manual/reference/operator/aggregation/collStats/>`_ with :symbol:`mongoc_collection_aggregate()` instead.
2121

2222
Parameters
2323
----------

src/libmongoc/doc/tutorial.rst

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -782,70 +782,27 @@ On Windows:
782782
Executing Commands
783783
------------------
784784

785-
The driver provides helper functions for executing MongoDB commands on client, database and collection structures. These functions return :doc:`cursors <mongoc_cursor_t>`; the ``_simple`` variants return booleans indicating success or failure.
785+
The driver provides helper functions for executing MongoDB commands on client, database and collection structures. The ``_simple`` variants return booleans indicating success or failure.
786786

787-
This example executes the `collStats <https://docs.mongodb.org/manual/reference/command/collStats/>`_ command against the collection "mycoll" in database "mydb".
787+
This example executes the `ping <https://docs.mongodb.org/manual/reference/command/ping/>`_ command against the database "mydb".
788788

789-
.. code-block:: c
790-
791-
#include <bson/bson.h>
792-
#include <mongoc/mongoc.h>
793-
#include <stdio.h>
794-
795-
int
796-
main (int argc, char *argv[])
797-
{
798-
mongoc_client_t *client;
799-
mongoc_collection_t *collection;
800-
bson_error_t error;
801-
bson_t *command;
802-
bson_t reply;
803-
char *str;
804-
805-
mongoc_init ();
806-
807-
client = mongoc_client_new (
808-
"mongodb://localhost:27017/?appname=executing-example");
809-
collection = mongoc_client_get_collection (client, "mydb", "mycoll");
810-
811-
command = BCON_NEW ("collStats", BCON_UTF8 ("mycoll"));
812-
if (mongoc_collection_command_simple (
813-
collection, command, NULL, &reply, &error)) {
814-
str = bson_as_canonical_extended_json (&reply, NULL);
815-
printf ("%s\n", str);
816-
bson_free (str);
817-
} else {
818-
fprintf (stderr, "Failed to run command: %s\n", error.message);
819-
}
820-
821-
bson_destroy (command);
822-
bson_destroy (&reply);
823-
mongoc_collection_destroy (collection);
824-
mongoc_client_destroy (client);
825-
mongoc_cleanup ();
826-
827-
return 0;
828-
}
789+
.. literalinclude:: ../examples/tutorial/executing.c
829790

830791
Compile the code and run it:
831792

832793
.. code-block:: none
833794
834795
$ gcc -o executing executing.c $(pkg-config --cflags --libs libmongoc-1.0)
835796
$ ./executing
836-
{ "ns" : "mydb.mycoll", "count" : 1, "size" : 48, "avgObjSize" : 48, "numExtents" : 1, "storageSize" : 8192,
837-
"lastExtentSize" : 8192.000000, "paddingFactor" : 1.000000, "userFlags" : 1, "capped" : false, "nindexes" : 1,
838-
"indexDetails" : { }, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1.000000 }
797+
{ "ok" : { "$numberDouble" : "1.0" }, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1682609211, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType" : "00" } }, "keyId" : { "$numberLong" : "0" } } }, "operationTime" : { "$timestamp" : { "t" : 1682609211, "i" : 1 } } }
839798
840799
On Windows:
841800

842801
.. code-block:: none
843802
844803
C:\> cl.exe /IC:\mongo-c-driver\include\libbson-1.0 /IC:\mongo-c-driver\include\libmongoc-1.0 executing.c
845804
C:\> executing
846-
{ "ns" : "mydb.mycoll", "count" : 1, "size" : 48, "avgObjSize" : 48, "numExtents" : 1, "storageSize" : 8192,
847-
"lastExtentSize" : 8192.000000, "paddingFactor" : 1.000000, "userFlags" : 1, "capped" : false, "nindexes" : 1,
848-
"indexDetails" : { }, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1.000000 }
805+
{ "ok" : { "$numberDouble" : "1.0" }, "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1682609211, "i" : 1 } }, "signature" : { "hash" : { "$binary" : { "base64" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType" : "00" } }, "keyId" : { "$numberLong" : "0" } } }, "operationTime" : { "$timestamp" : { "t" : 1682609211, "i" : 1 } } }
849806
850807
Threading
851808
---------
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// BEGIN:mongoc_collection_command_simple
2+
#include <bson/bson.h>
3+
#include <mongoc/mongoc.h>
4+
#include <stdio.h>
5+
6+
static void
7+
do_ping (mongoc_collection_t *collection)
8+
{
9+
bson_error_t error;
10+
bson_t *cmd;
11+
bson_t reply;
12+
char *str;
13+
14+
cmd = BCON_NEW ("ping", BCON_INT32 (1));
15+
16+
if (mongoc_collection_command_simple (
17+
collection, cmd, NULL, &reply, &error)) {
18+
str = bson_as_canonical_extended_json (&reply, NULL);
19+
printf ("Got reply: %s\n", str);
20+
bson_free (str);
21+
} else {
22+
fprintf (stderr, "Got error: %s\n", error.message);
23+
}
24+
25+
bson_destroy (&reply);
26+
bson_destroy (cmd);
27+
}
28+
// END:mongoc_collection_command_simple
29+
30+
int
31+
main (int argc, char **argv)
32+
{
33+
bson_error_t error;
34+
char *uri_string;
35+
mongoc_uri_t *uri;
36+
mongoc_client_t *client;
37+
mongoc_collection_t *coll;
38+
39+
mongoc_init ();
40+
41+
if (argc != 2) {
42+
MONGOC_ERROR ("Error: expected URI argument.\n"
43+
"Usage: %s <MongoDB URI>\n"
44+
"Example: %s mongodb://localhost:27017",
45+
argv[0],
46+
argv[0]);
47+
return EXIT_FAILURE;
48+
}
49+
uri_string = argv[1];
50+
uri = mongoc_uri_new_with_error (uri_string, &error);
51+
if (!uri) {
52+
MONGOC_ERROR (
53+
"failed to parse URI: %s\nError: %s", uri_string, error.message);
54+
return EXIT_FAILURE;
55+
}
56+
57+
client = mongoc_client_new_from_uri (uri);
58+
if (!client) {
59+
MONGOC_ERROR ("failed to create client");
60+
return EXIT_FAILURE;
61+
}
62+
63+
coll = mongoc_client_get_collection (client, "db", "coll");
64+
do_ping (coll);
65+
mongoc_collection_destroy (coll);
66+
mongoc_uri_destroy (uri);
67+
mongoc_client_destroy (client);
68+
mongoc_cleanup ();
69+
return EXIT_SUCCESS;
70+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bson/bson.h>
2+
#include <mongoc/mongoc.h>
3+
#include <stdio.h>
4+
5+
int
6+
main (int argc, char *argv[])
7+
{
8+
mongoc_client_t *client;
9+
bson_error_t error;
10+
bson_t *command;
11+
bson_t reply;
12+
char *str;
13+
14+
mongoc_init ();
15+
16+
client = mongoc_client_new (
17+
"mongodb://localhost:27017/?appname=executing-example");
18+
19+
command = BCON_NEW ("ping", BCON_INT32 (1));
20+
if (mongoc_client_command_simple (
21+
client, "mydb", command, NULL, &reply, &error)) {
22+
str = bson_as_canonical_extended_json (&reply, NULL);
23+
printf ("%s\n", str);
24+
bson_free (str);
25+
} else {
26+
fprintf (stderr, "Failed to run command: %s\n", error.message);
27+
}
28+
29+
bson_destroy (command);
30+
bson_destroy (&reply);
31+
mongoc_client_destroy (client);
32+
mongoc_cleanup ();
33+
34+
return 0;
35+
}

src/libmongoc/tests/json/client_side_encryption/legacy/bypassedCommand.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
]
7979
},
8080
{
81-
"description": "current op is not bypassed",
81+
"description": "kill op is not bypassed",
8282
"clientOptions": {
8383
"autoEncryptOpts": {
8484
"kmsProviders": {
@@ -90,14 +90,15 @@
9090
{
9191
"name": "runCommand",
9292
"object": "database",
93-
"command_name": "currentOp",
93+
"command_name": "killOp",
9494
"arguments": {
9595
"command": {
96-
"currentOp": 1
96+
"killOp": 1,
97+
"op": 1234
9798
}
9899
},
99100
"result": {
100-
"errorContains": "command not supported for auto encryption: currentOp"
101+
"errorContains": "command not supported for auto encryption: killOp"
101102
}
102103
}
103104
]

src/libmongoc/tests/test-mongoc-collection.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,8 +3402,9 @@ test_rename (void)
34023402

34033403

34043404
static void
3405-
test_stats (void)
3405+
test_stats (void *unused)
34063406
{
3407+
BSON_UNUSED (unused);
34073408
mongoc_collection_t *collection;
34083409
mongoc_client_t *client;
34093410
bson_error_t error;
@@ -6296,7 +6297,14 @@ test_collection_install (TestSuite *suite)
62966297
NULL,
62976298
test_framework_skip_if_slow_or_live);
62986299
TestSuite_AddLive (suite, "/Collection/rename", test_rename);
6299-
TestSuite_AddLive (suite, "/Collection/stats", test_stats);
6300+
// The collStats command is deprecated in MongoDB 6.0 (maxWireVersion=17) and
6301+
// may be removed in a future major release.
6302+
TestSuite_AddFull (suite,
6303+
"/Collection/stats",
6304+
test_stats,
6305+
NULL,
6306+
NULL,
6307+
test_framework_skip_if_max_wire_version_more_than_17);
63006308
TestSuite_AddMockServerTest (
63016309
suite, "/Collection/stats/read_pref", test_stats_read_pref);
63026310
TestSuite_AddMockServerTest (

src/libmongoc/tests/test-mongoc-sample-commands.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,22 +3409,6 @@ test_sample_run_command (mongoc_database_t *db)
34093409
bson_destroy (&reply);
34103410
/* End runCommand Example 1 */
34113411

3412-
/* Start runCommand Example 2 */
3413-
run_command = BCON_NEW ("collStats", BCON_UTF8 ("restaurants"));
3414-
3415-
r = mongoc_database_write_command_with_opts (
3416-
db, run_command, NULL /* opts */, &reply, &error);
3417-
bson_destroy (run_command);
3418-
3419-
if (!r) {
3420-
MONGOC_ERROR ("%s\n", error.message);
3421-
}
3422-
3423-
/* Do something with reply here */
3424-
3425-
bson_destroy (&reply);
3426-
/* End runCommand Example 2 */
3427-
34283412
ASSERT_NO_CAPTURED_LOGS ("sample runCommand examples");
34293413
}
34303414

0 commit comments

Comments
 (0)