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 β€” a mongoc_collection_t. your collection, your main character ✨
  • filter β€” a bson_t holding the filter. this is the "which docs are we counting" vibe check πŸ¦‹
  • opts β€” a bson_t, or NULL to just ignore it entirely (we love a low-effort option)
  • read_prefs β€” a mongoc_read_prefs_t or NULL
  • reply β€” a maybe-NULL pointer to overwritable storage for a bson_t that'll hold the results 🫢
  • error β€” an optional spot for a bson_error_t, or NULL

and now the tea β˜• β€” opts can be NULL, or a BSON document stuffed with extra command options:

  • readConcern β€” build yourself a mongoc_read_concern_t and use mongoc_read_concern_append() to add the read concern onto opts. the example code for mongoc_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 a mongoc_client_session_t with mongoc_client_start_session(). then you can begin a transaction with mongoc_client_session_start_transaction(), optionally passing a mongoc_transaction_opt_t that overrides the options inherited from collection, and use mongoc_client_session_append() to add the session to opts. the example code for mongoc_client_session_t has 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 calling mongoc_client_select_server(), then mongoc_server_description_id() on what it gives back πŸ‘‘
  • skip β€” an int saying how many documents matching the query get skipped before the counting even starts
  • limit β€” an int for the maximum number of documents to count
  • comment β€” a bson_value_t with the comment you wanna attach to this command. it'll show up in log messages, profiler output, and currentOp output. 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

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