Get the number of hits from a mongo-c call?

    bson_error_t error;
    bson_t *bson_search = bson_new_from_json (json, strlen(json), &error);
    if (!bson_search)
    {
        log_fatal(error.message);
    }

    mongoc_cursor_t *cursor;
    cursor = mongoc_collection_aggregate(client.collection, MONGOC_QUERY_NONE, bson_search, NULL, NULL);

    int i = 0;
    char *str;
    const bson_t *doc;
    while (mongoc_cursor_next (cursor, &doc)) 
    {
        str = bson_as_json (doc, NULL);        
        printf ("%s\n", str);
        bson_free (str);
        i++;
    }

    if (mongoc_cursor_error (cursor, &error)) 
    {
        fprintf (stderr, "Cursor Failure: %s\n", error.message);
    }

    mongoc_cursor_destroy (cursor);
    bson_destroy (bson_search);

According to this page that documents the mongo_cursor_t,

https://mongoc.org/libmongoc/current/mongoc_cursor_t.html

the cursor is for an unknown number of documents. Is there an equivalent cursor or function call I can use to get the number of hits? I’d ultimately like to store the documents in a char* or concatenated as a single char*.

If there’s a way I can just get full raw json as a char*, that’s fine too (even preferred).

I can’t answer my original question of identifying the number of documents (which I think is a really bad API limitation), since I skimmed through the source code and it seems like the cursor is actually a linked list. However, I can reach my goal of getting the documents into a single char*.

Idea: Convert the search to a bson via an array of bson under { “documents” : [ array of objects ] }
If anyone has a cleaner solution or suggestions on how to simplify this, I would appreciate it.

    mongoc_cursor_t *cursor;
    cursor = mongoc_collection_aggregate(client.collection, MONGOC_QUERY_NONE, bson_search, NULL, NULL);
    if (mongoc_cursor_error (cursor, &error))
    {
        log_fatal("Cursor Failure: %s", error.message);
    }

    // convert cursor (linked-list) to bson
    int i = 0;
    const bson_t *doc;
    bson_array_builder_t *array_docs = bson_array_builder_new();
    while (mongoc_cursor_next(cursor, &doc)) 
    {
        if (!bson_array_builder_append_document(array_docs, doc))
        {
            // Handle failure to concatenate
        }
        log_debug("%d", i);
        i++;
    }
    bson_t documents;
    bson_array_builder_build(array_docs, &documents);
    bson_t * bson_out = bson_new();
    bson_append_array(bson_out, "documents", -1, &documents);

    // bson -> char* via copy
    char *out = bson_as_json(bson_out, NULL);

    // Free
    mongoc_cursor_destroy (cursor);
    bson_destroy (bson_search);
    bson_destroy (documents);
    bson_destroy(bson_out);

You can use $count stage in your aggregation pipeline to find out the count you are looking for.
See an example here - mongo-c-driver/src/libmongoc/tests/test-mongoc-sample-commands.c at 7cbea30c4ccf6c6e6872f6e3a202eb68ff4348d9 · mongodb/mongo-c-driver · GitHub

Thanks for the suggestion.