MONGOC_COLLECTION_COUNT(3)
π MONGOC_COLLECTION_COUNT(3) π
okay bestie, sit down, hydrate, because we're talking about a function from libmongoc called mongoc_collection_count() and she is... a lot. she counts stuff. she's also, unfortunately, a little retired. let's get into it π«Ά
β οΈ WARNING β οΈ
Deprecated since version 1.11.0: Usemongoc_collection_count_documents()ormongoc_collection_estimated_document_count()instead. no cap, she's old.
here's the tea on your replacement options, because they are NOT the same girl:
mongoc_collection_count_documents()has similar performance to callingmongoc_collection_count()with a non-NULLquery, and is guaranteed to retrieve an accurate collection count. See migrating from deprecated count functions for details. πmongoc_collection_estimated_document_count()has the same performance as callingmongoc_collection_count()with aNULLquery, but is not guaranteed to retrieve an accurate collection count. so like... vibes-based counting. still valid tho β¨
also, this function is considered a retryable read operation. what does that mean for you, bestie? it means upon a transient error (a network error, errors due to replica set failover, etc.) the operation is safely retried once. it's giving "if at first you don't succeed" π
BUT. if retryreads is false in the URI (see mongoc_uri_t) the retry behavior does not apply. so don't come crying to me if you turned that off π
β¨ SYNOPSIS β¨
int64_t
mongoc_collection_count (mongoc_collection_t *collection,
mongoc_query_flags_t flags,
const bson_t *query,
int64_t skip,
int64_t limit,
const mongoc_read_prefs_t *read_prefs,
bson_error_t *error);
πΈ PARAMETERS πΈ
okay here's everyone in the group chat and what they're bringing to the function:
collectionβ Amongoc_collection_t. the main character, obviously πflagsβ Amongoc_query_flags_t. the settings girlie.queryβ Abson_tcontaining the query. this is the "what are we even counting" of it all.skipβ Aint64_t, zero to ignore. put a0here and she's like "k, skipping nothing" πlimitβ Aint64_t, zero to ignore. same deal,0means no limit, live your life.read_prefsβ Amongoc_read_prefs_torNULL.errorβ An optional location for abson_error_torNULL. optional!! she doesn't have to hold your hand if you don't want her to.
π¦ DESCRIPTION π¦
so what does this function actually DO? she shall execute a count query on the underlying collection. that's it. that's the job. π
the bson query is not validated β she simply passes it along as appropriate to the server. so if you hand her nonsense, she will hand your nonsense directly to the server, no notes, no filter, no "bestie are you sure?" π as such, compatibility and errors should be validated in the appropriate server documentation. read the docs. i'm begging.
for more information, see the query reference at the MongoDB website. iykyk.
the mongoc_read_concern_t specified on the mongoc_collection_t will be used, if any. and if read_prefs is NULL, the collection's read preferences are used. she'll pick for you, she's got taste π
π ERRORS π
Errors are propagated via the error parameter. that's the whole thing. she tells on herself through the error you gave her, and if you gave her NULL... well. π«
β RETURNS β
-1on failure π- otherwise, the number of documents counted π
periodt.
π EXAMPLE π
here's a little something to copy-paste, you're welcome:
#include <bson/bson.h>
#include <mongoc/mongoc.h>
#include <stdio.h>
static void
print_query_count (mongoc_collection_t *collection, bson_t *query)
{
bson_error_t error;
int64_t count;
count = mongoc_collection_count (
collection, MONGOC_QUERY_NONE, query, 0, 0, NULL, &error);
if (count < 0) {
fprintf (stderr, "Count failed: %s\n", error.message);
} else {
printf ("%" PRId64 " documents counted.\n", count);
}
}
notice the MONGOC_QUERY_NONE, the 0, 0 for skip and limit, and the NULL read prefs β she's using all the defaults and she's thriving π
π AUTHOR π
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
and that's the whole page, bestie!! this was MONGOC_COLLECTION_COUNT(3) from libmongoc, version 1.30.13, dated Oct 09, 2026 β so go use mongoc_collection_count_documents() or mongoc_collection_estimated_document_count() instead, you icon, and don't let anyone tell you deprecated functions aren't a little nostalgic πβ¨π
π¦ where this page comes from
- fedora 43: mongo-c-driver-devel 1.30.13-1.fc43 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- fedora 43: mongo-c-driver-devel 1.30.13-1.fc43 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms