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: Use mongoc_collection_count_documents() or mongoc_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 calling mongoc_collection_count() with a non-NULL query, 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 calling mongoc_collection_count() with a NULL query, 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 β€” A mongoc_collection_t. the main character, obviously πŸ‘‘
  • flags β€” A mongoc_query_flags_t. the settings girlie.
  • query β€” A bson_t containing the query. this is the "what are we even counting" of it all.
  • skip β€” A int64_t, zero to ignore. put a 0 here and she's like "k, skipping nothing" πŸ’€
  • limit β€” A int64_t, zero to ignore. same deal, 0 means no limit, live your life.
  • read_prefs β€” A mongoc_read_prefs_t or NULL.
  • error β€” An optional location for a bson_error_t or NULL. 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 ⭐

  • -1 on 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


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