MONGOC_COLLECT...DOCUMENT_COUNT(3)
okay so this is libmongoc's page for MONGOC_COLLECT...DOCUMENT_COUNT(3), and we're gonna get you comfy with it π bestie, it's all about mongoc_collection_estimated_document_count() and yes, the βestimatedβ part is doing real work here.
π SYNOPSIS π
int64_t
mongoc_collection_estimated_document_count (mongoc_collection_t *collection,
const bson_t *opts,
const mongoc_read_prefs_t *read_prefs,
bson_t *reply,
bson_error_t *error);
π PARAMETERS π
collectionβ amongoc_collection_t. the main character we're counting stuff in πoptsβ abson_t, orNULLif you wanna ignore it.read_prefsβ amongoc_read_prefs_torNULL.replyβ a maybe-NULLpointer to overwritable storage for abson_twhere the results go.errorβ an optional spot for abson_error_t, orNULL.
opts can be NULL, or a BSON document with extra command options:
readConcernβ build amongoc_read_concern_tand usemongoc_read_concern_append()to add the read concern toopts. there's example code formongoc_client_read_command_with_opts(). read concern needs MongoDB 3.2 or newer, otherwise you get an error.sessionIdβ first make amongoc_client_session_twithmongoc_client_start_session(). you can start a transaction withmongoc_client_session_start_transaction(), optionally with amongoc_transaction_opt_tthat overrides the options inherited fromcollection, then usemongoc_client_session_append()to add the session toopts. example code lives withmongoc_client_session_t.collationβ sets up textual comparisons. check out Setting Collation Order and the MongoDB Manual entry on Collation. collation needs MongoDB 3.2 or newer, otherwise: error time πserverIdβ wanna target one specific server? include an int32"serverId"field. get that id by callingmongoc_client_select_server(), thenmongoc_server_description_id()on what it returns.skipβ an int saying how many documents matching the query should be skipped before counting.limitβ an int capping the max number of documents to count.commentβ abson_value_tspecifying the comment to attach to this command. the comment will appear in log messages, profiler output, andcurrentOpoutput. requires MongoDB 4.4 or later.
want the full menu? see the MongoDB Manual entry on the count command. iykyk β¨
π DESCRIPTION π
this function runs a count query on collection. in contrast with mongoc_collection_count_documents(), the count it gives you is not guaranteed to be accurate. so likeβ¦ it's giving estimate, not gospel π₯
this one is a retryable read operation. upon a transient error (a network error, errors due to replica set failover, etc.), the operation is safely retried once. if retryreads is false in the URI (see mongoc_uri_t), the retry behavior does not apply.
β¨ Behavior β¨
this method is implemented using the count command.
ok important lore: due to an oversight in versions5.0.0-5.0.8of MongoDB, thecountcommand was not included in version"1"of the Stable API. applications using this method with the Stable API are recommended to upgrade their server version to5.0.9+or disable strict mode (viamongoc_server_api_strict()) to avoid encountering errors. no cap, read that twice if you need to π«Ά
π ERRORS π
errors are propagated via the error parameter.
π«Ά RETURNS π«Ά
-1 on failure, otherwise the number of documents counted. slay β
π EXAMPLE π
#include <bson/bson.h>
#include <mongoc/mongoc.h>
#include <stdio.h>
static void
print_count (mongoc_collection_t *collection, bson_t *query)
{
bson_error_t error;
int64_t count;
bson_t* opts = BCON_NEW ("skip", BCON_INT64(5));
count = mongoc_collection_estimated_document_count (
collection, opts, NULL, NULL, &error);
bson_destroy (opts);
if (count < 0) {
fprintf (stderr, "Count failed: %s\n", error.message);
} else {
printf ("%" PRId64 " documents counted.\n", count);
}
}
π SEE ALSO π
mongoc_collection_count_documents()Count: Behavior in the MongoDB Manual
π¦ AUTHOR π¦
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
okay that's the whole page, bestie β it's libmongoc version 2.5.6, dated Oct 09, 2024. mwah πβ¨
π¦ where this page comes from
- epel 10: mongo-c-driver-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms