Skip to content

Commit 99e9893

Browse files
authored
CDRIVER-4309: always use count command for estimatedDocumentCount (#984)
* always use count command for estimatedDocumentCount * Update spec tests for estimatedDocumentCount Synced with mongodb/specifications@021cbc8 Includes some test changes for CDRIVER-4063 and CDRIVER-4330 * Ignore "ns not found" error for dropCollection operations This requires setting MONGOC_ERROR_API_VERSION_2 on clients under test. It was previously only set on the internal client. * Skip comment option tests pending CDRIVER-4199
1 parent b2c2fc5 commit 99e9893

File tree

48 files changed

+7779
-1594
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+7779
-1594
lines changed

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

Lines changed: 17 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -790,57 +790,6 @@ mongoc_collection_count_with_opts (
790790
RETURN (ret);
791791
}
792792

793-
/* --------------------------------------------------------------------------
794-
*
795-
* _make_aggregate_for_edc --
796-
*
797-
* Construct an aggregate pipeline with the following form:
798-
*
799-
*
800-
* { pipeline: [
801-
* { $collStats: { count: {} } },
802-
* { $group: { _id: 1, n: { $sum: $count } } },
803-
* ]
804-
* }
805-
*
806-
*--------------------------------------------------------------------------
807-
*/
808-
static void
809-
_make_aggregate_for_edc (const mongoc_collection_t *coll, bson_t *out)
810-
{
811-
bson_t pipeline;
812-
bson_t coll_stats_stage;
813-
bson_t coll_stats_stage_doc;
814-
bson_t group_stage;
815-
bson_t group_stage_doc;
816-
bson_t sum;
817-
bson_t cursor_empty;
818-
bson_t empty;
819-
820-
BSON_APPEND_UTF8 (out, "aggregate", coll->collection);
821-
BSON_APPEND_DOCUMENT_BEGIN (out, "cursor", &cursor_empty);
822-
bson_append_document_end (out, &cursor_empty);
823-
BSON_APPEND_ARRAY_BEGIN (out, "pipeline", &pipeline);
824-
825-
BSON_APPEND_DOCUMENT_BEGIN (&pipeline, "0", &coll_stats_stage);
826-
BSON_APPEND_DOCUMENT_BEGIN (
827-
&coll_stats_stage, "$collStats", &coll_stats_stage_doc);
828-
BSON_APPEND_DOCUMENT_BEGIN (&coll_stats_stage_doc, "count", &empty);
829-
bson_append_document_end (&coll_stats_stage_doc, &empty);
830-
bson_append_document_end (&coll_stats_stage, &coll_stats_stage_doc);
831-
bson_append_document_end (&pipeline, &coll_stats_stage);
832-
833-
BSON_APPEND_DOCUMENT_BEGIN (&pipeline, "1", &group_stage);
834-
BSON_APPEND_DOCUMENT_BEGIN (&group_stage, "$group", &group_stage_doc);
835-
BSON_APPEND_INT32 (&group_stage_doc, "_id", 1);
836-
BSON_APPEND_DOCUMENT_BEGIN (&group_stage_doc, "n", &sum);
837-
BSON_APPEND_UTF8 (&sum, "$sum", "$count");
838-
bson_append_document_end (&group_stage_doc, &sum);
839-
bson_append_document_end (&group_stage, &group_stage_doc);
840-
bson_append_document_end (&pipeline, &group_stage);
841-
bson_append_array_end (out, &pipeline);
842-
}
843-
844793
int64_t
845794
mongoc_collection_estimated_document_count (
846795
mongoc_collection_t *coll,
@@ -878,48 +827,23 @@ mongoc_collection_estimated_document_count (
878827
}
879828

880829
reply_ptr = reply ? reply : &reply_local;
881-
if (server_stream->sd->max_wire_version < WIRE_VERSION_4_9) {
882-
/* On < 4.9, use actual count command for estimatedDocumentCount */
883-
BSON_APPEND_UTF8 (&cmd, "count", coll->collection);
884-
ret = _mongoc_client_command_with_opts (coll->client,
885-
coll->db,
886-
&cmd,
887-
MONGOC_CMD_READ,
888-
opts,
889-
MONGOC_QUERY_NONE,
890-
read_prefs,
891-
coll->read_prefs,
892-
coll->read_concern,
893-
coll->write_concern,
894-
reply_ptr,
895-
error);
896-
if (ret) {
897-
if (bson_iter_init_find (&iter, reply_ptr, "n")) {
898-
count = bson_iter_as_int64 (&iter);
899-
}
900-
}
901-
} else {
902-
/* On >= 4.9, use aggregate with collStats for estimatedDocumentCount */
903-
_make_aggregate_for_edc (coll, &cmd);
904-
ret = mongoc_collection_read_command_with_opts (
905-
coll, &cmd, read_prefs, opts, reply_ptr, error);
906-
907-
if (!ret && error->code == MONGOC_ERROR_COLLECTION_DOES_NOT_EXIST) {
908-
/* Collection does not exist. From spec: return 0 but no err:
909-
* https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#estimateddocumentcount
910-
*/
911-
if (reply) {
912-
bson_reinit (reply);
913-
}
914-
memset (error, 0, sizeof *error);
915-
count = 0;
916-
GOTO (done);
917-
}
918-
if (ret && bson_iter_init (&iter, reply_ptr)) {
919-
if (bson_iter_find_descendant (
920-
&iter, "cursor.firstBatch.0.n", &iter)) {
921-
count = bson_iter_as_int64 (&iter);
922-
}
830+
831+
BSON_APPEND_UTF8 (&cmd, "count", coll->collection);
832+
ret = _mongoc_client_command_with_opts (coll->client,
833+
coll->db,
834+
&cmd,
835+
MONGOC_CMD_READ,
836+
opts,
837+
MONGOC_QUERY_NONE,
838+
read_prefs,
839+
coll->read_prefs,
840+
coll->read_concern,
841+
coll->write_concern,
842+
reply_ptr,
843+
error);
844+
if (ret) {
845+
if (bson_iter_init_find (&iter, reply_ptr, "n")) {
846+
count = bson_iter_as_int64 (&iter);
923847
}
924848
}
925849

src/libmongoc/tests/json/crud/unified/aggregate-out-readConcern.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"description": "aggregate-out-readConcern",
3-
"schemaVersion": "1.0",
3+
"schemaVersion": "1.4",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.1.0",
77
"topologies": [
88
"replicaset",
99
"sharded"
10-
]
10+
],
11+
"serverless": "forbid"
1112
}
1213
],
1314
"createEntities": [

0 commit comments

Comments
 (0)