Skip to content

CDRIVER-4309: always use count command for estimatedDocumentCount #984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 17 additions & 93 deletions src/libmongoc/src/mongoc/mongoc-collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,57 +790,6 @@ mongoc_collection_count_with_opts (
RETURN (ret);
}

/* --------------------------------------------------------------------------
*
* _make_aggregate_for_edc --
*
* Construct an aggregate pipeline with the following form:
*
*
* { pipeline: [
* { $collStats: { count: {} } },
* { $group: { _id: 1, n: { $sum: $count } } },
* ]
* }
*
*--------------------------------------------------------------------------
*/
static void
_make_aggregate_for_edc (const mongoc_collection_t *coll, bson_t *out)
{
bson_t pipeline;
bson_t coll_stats_stage;
bson_t coll_stats_stage_doc;
bson_t group_stage;
bson_t group_stage_doc;
bson_t sum;
bson_t cursor_empty;
bson_t empty;

BSON_APPEND_UTF8 (out, "aggregate", coll->collection);
BSON_APPEND_DOCUMENT_BEGIN (out, "cursor", &cursor_empty);
bson_append_document_end (out, &cursor_empty);
BSON_APPEND_ARRAY_BEGIN (out, "pipeline", &pipeline);

BSON_APPEND_DOCUMENT_BEGIN (&pipeline, "0", &coll_stats_stage);
BSON_APPEND_DOCUMENT_BEGIN (
&coll_stats_stage, "$collStats", &coll_stats_stage_doc);
BSON_APPEND_DOCUMENT_BEGIN (&coll_stats_stage_doc, "count", &empty);
bson_append_document_end (&coll_stats_stage_doc, &empty);
bson_append_document_end (&coll_stats_stage, &coll_stats_stage_doc);
bson_append_document_end (&pipeline, &coll_stats_stage);

BSON_APPEND_DOCUMENT_BEGIN (&pipeline, "1", &group_stage);
BSON_APPEND_DOCUMENT_BEGIN (&group_stage, "$group", &group_stage_doc);
BSON_APPEND_INT32 (&group_stage_doc, "_id", 1);
BSON_APPEND_DOCUMENT_BEGIN (&group_stage_doc, "n", &sum);
BSON_APPEND_UTF8 (&sum, "$sum", "$count");
bson_append_document_end (&group_stage_doc, &sum);
bson_append_document_end (&group_stage, &group_stage_doc);
bson_append_document_end (&pipeline, &group_stage);
bson_append_array_end (out, &pipeline);
}

int64_t
mongoc_collection_estimated_document_count (
mongoc_collection_t *coll,
Expand Down Expand Up @@ -878,48 +827,23 @@ mongoc_collection_estimated_document_count (
}

reply_ptr = reply ? reply : &reply_local;
if (server_stream->sd->max_wire_version < WIRE_VERSION_4_9) {
/* On < 4.9, use actual count command for estimatedDocumentCount */
BSON_APPEND_UTF8 (&cmd, "count", coll->collection);
ret = _mongoc_client_command_with_opts (coll->client,
coll->db,
&cmd,
MONGOC_CMD_READ,
opts,
MONGOC_QUERY_NONE,
read_prefs,
coll->read_prefs,
coll->read_concern,
coll->write_concern,
reply_ptr,
error);
if (ret) {
if (bson_iter_init_find (&iter, reply_ptr, "n")) {
count = bson_iter_as_int64 (&iter);
}
}
} else {
/* On >= 4.9, use aggregate with collStats for estimatedDocumentCount */
_make_aggregate_for_edc (coll, &cmd);
ret = mongoc_collection_read_command_with_opts (
coll, &cmd, read_prefs, opts, reply_ptr, error);

if (!ret && error->code == MONGOC_ERROR_COLLECTION_DOES_NOT_EXIST) {
/* Collection does not exist. From spec: return 0 but no err:
* https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#estimateddocumentcount
*/
if (reply) {
bson_reinit (reply);
}
memset (error, 0, sizeof *error);
count = 0;
GOTO (done);
}
if (ret && bson_iter_init (&iter, reply_ptr)) {
if (bson_iter_find_descendant (
&iter, "cursor.firstBatch.0.n", &iter)) {
count = bson_iter_as_int64 (&iter);
}

BSON_APPEND_UTF8 (&cmd, "count", coll->collection);
ret = _mongoc_client_command_with_opts (coll->client,
coll->db,
&cmd,
MONGOC_CMD_READ,
opts,
MONGOC_QUERY_NONE,
read_prefs,
coll->read_prefs,
coll->read_concern,
coll->write_concern,
reply_ptr,
error);
if (ret) {
if (bson_iter_init_find (&iter, reply_ptr, "n")) {
count = bson_iter_as_int64 (&iter);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"description": "aggregate-out-readConcern",
"schemaVersion": "1.0",
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "4.1.0",
"topologies": [
"replicaset",
"sharded"
]
],
"serverless": "forbid"
}
],
"createEntities": [
Expand Down
Loading