mongo script to find collection size in database

Here is a mongo script in C to find the collection size in a database:

#include <mongoc/mongoc.h>

int main() {
    mongoc_client_t *client;
    mongoc_collection_t *collection;
    bson_t *command, reply;
    bson_error_t error;
    bool retval;

    mongoc_init();

    client = mongoc_client_new("mongodb://localhost:27017");
    collection = mongoc_client_get_collection(client, "your_database_name", "your_collection_name");

    command = BCON_NEW("collStats", BCON_UTF8("your_collection_name"));

    retval = mongoc_collection_command_simple(
        collection, command, NULL, &reply, &error);

    if (!retval) {
        fprintf(stderr, "Failed to execute command: %s\n", error.message);
        return EXIT_FAILURE;
    }

    bson_iter_t iter;
    bson_iter_init(&iter, &reply);

    while (bson_iter_next(&iter)) {
        const char *key = bson_iter_key(&iter);
        const bson_value_t *value = bson_iter_value(&iter);

        if (strcmp(key, "size") == 0) {
            printf("Collection size: %lld\n", value->value.v_int64);
        }
    }

    bson_destroy(command);
    bson_destroy(&reply);
    mongoc_collection_destroy(collection);
    mongoc_client_destroy(client);
    mongoc_cleanup();

    return EXIT_SUCCESS;
}

Explanation for each step:

  1. mongoc_client_t *client;: Declares a pointer to a MongoDB client object.
  2. mongoc_collection_t *collection;: Declares a pointer to a MongoDB collection object.
  3. bson_t *command, reply;: Declares pointers to BSON (Binary JSON) objects for the command and reply.
  4. mongoc_init();: Initializes the MongoDB C driver.
  5. client = mongoc_client_new("mongodb://localhost:27017");: Creates a new MongoDB client connected to the specified server.
  6. collection = mongoc_client_get_collection(client, "your_database_name", "your_collection_name");: Retrieves a MongoDB collection object from the specified database and collection name.
  7. command = BCON_NEW("collStats", BCON_UTF8("your_collection_name"));: Creates a BSON document representing the collStats command with the specified collection name.
  8. retval = mongoc_collection_command_simple(collection, command, NULL, &reply, &error);: Executes the command on the collection and stores the result in the reply object.
  9. bson_iter_t iter; bson_iter_init(&iter, &reply);: Initializes an iterator for iterating over the fields in the reply.
  10. while (bson_iter_next(&iter)) { ... }: Loops through each field in the reply.
  11. const char *key = bson_iter_key(&iter);: Retrieves the key (field name) of the current field.
  12. const bson_value_t *value = bson_iter_value(&iter);: Retrieves the value of the current field.
  13. if (strcmp(key, "size") == 0) { ... }: Checks if the current field is the "size" field.
  14. printf("Collection size: %lld\n", value->value.v_int64);: Prints the value of the "size" field.
  15. bson_destroy(command); bson_destroy(&reply);: Frees the memory used by the command and reply BSON objects.
  16. mongoc_collection_destroy(collection); mongoc_client_destroy(client);: Releases the resources used by the collection and client objects.
  17. mongoc_cleanup();: Cleans up the MongoDB C driver.

Note: Make sure to replace "your_database_name" and "your_collection_name" with the actual names of your database and collection.