MONGOC_COLLEC...OUNT_DOCUMENTS(3)
β¨ MONGOC_COLLEC...OUNT_DOCUMENTS β¨
okay so bestie, you're looking at the libmongoc manual page for mongoc_collection_count_documents β the function that actually counts your documents for real, no vibes-based estimation π
πΈ SYNOPSIS πΈ
int64_t
mongoc_collection_count_documents (mongoc_collection_t *collection,
const bson_t *filter,
const bson_t *opts,
const mongoc_read_prefs_t *read_prefs,
bson_t *reply,
bson_error_t *error);
π PARAMETERS π
collectionβ amongoc_collection_t. your collection, your main character β¨filterβ abson_tholding the filter. this is the "which docs are we counting" vibe check π¦optsβ abson_t, orNULLto just ignore it entirely (we love a low-effort option)read_prefsβ amongoc_read_prefs_torNULLreplyβ a maybe-NULLpointer to overwritable storage for abson_tthat'll hold the results π«Άerrorβ an optional spot for abson_error_t, orNULL
and now the tea β β opts can be NULL, or a BSON document stuffed with extra command options:
readConcernβ build yourself amongoc_read_concern_tand usemongoc_read_concern_append()to add the read concern ontoopts. the example code formongoc_client_read_command_with_opts()is right there if you need it. heads up: read concern needs MongoDB 3.2 or later, otherwise you get an error, no cap πsessionIdβ first, construct amongoc_client_session_twithmongoc_client_start_session(). then you can begin a transaction withmongoc_client_session_start_transaction(), optionally passing amongoc_transaction_opt_tthat overrides the options inherited fromcollection, and usemongoc_client_session_append()to add the session toopts. the example code formongoc_client_session_thas you covered π§collationβ configures textual comparisons. peep Setting Collation Order, plus the MongoDB Manual entry on Collation. collation needs MongoDB 3.2 or later, otherwise it's an error π₯serverIdβ wanna target one specific server? drop in an int32"serverId"field. get the id by callingmongoc_client_select_server(), thenmongoc_server_description_id()on what it gives back πskipβ an int saying how many documents matching the query get skipped before the counting even startslimitβ an int for the maximum number of documents to countcommentβ abson_value_twith the comment you wanna attach to this command. it'll show up in log messages, profiler output, andcurrentOpoutput. only string values are supported prior to MongoDB 4.4 βhintβ a document or a string specifying which index to use to support the query predicate
any other options? they get included in the aggregate command that gets sent. for the full list of literally everything, see the MongoDB Manual entry on the aggregate command π
π DESCRIPTION π
this function executes a count query on collection. and here's the difference from mongoc_collection_estimated_document_count(): the count you get back is guaranteed to be accurate. no guessing, no math, the real number π’β¨
it's also considered a retryable read operation. so when a transient error hits (network error, replica set failover drama, etc.), the operation is safely retried once. but if retryreads is false in the URI (see mongoc_uri_t), the retry behavior does not apply. sad but true π
π ERRORS π
errors get propagated via the error parameter. that's the whole section, she's brief and to the point π
π RETURNS π
-1 on failure π β otherwise, the number of documents counted π
π¦ EXAMPLE π¦
#include <bson/bson.h>
#include <mongoc/mongoc.h>
#include <stdio.h>
static void
print_count (mongoc_collection_t *collection, bson_t *filter)
{
bson_error_t error;
int64_t count;
bson_t* opts = BCON_NEW ("skip", BCON_INT64(5));
count = mongoc_collection_count_documents (
collection, filter, 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_estimated_document_count()
π AUTHOR π
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
and that's the whole page, bestie β written by MongoDB, Inc, brought to you by libmongoc version 2.5.6, dated Oct 09, 2026. slay, go count some documents π
β¨π
π¦ 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