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