📜  Mongo show db - C 编程语言(1)

📅  最后修改于: 2023-12-03 15:17:41.367000             🧑  作者: Mango

Mongo Show DB with C Programming Language

MongoDB is a popular NoSQL database that allows for flexible and efficient storage and retrieval of data. In this tutorial, we will show you how to use C Programming Language to connect to a MongoDB database and display the available databases.

Prerequisites

Before we start, ensure that you have the following components installed:

  • MongoDB C Driver (libmongoc)
  • GCC compiler
  • MongoDB instance running on localhost
Connecting to the MongoDB Instance

To connect to the MongoDB instance, we first include the necessary libraries and define the database URL.

#include <stdio.h>
#include <bson/bson.h>
#include <mongoc/mongoc.h>

int main() {
    mongoc_client_t *client;
    mongoc_collection_t *collection;
    mongoc_cursor_t *cursor;
    const bson_t *doc;
    bson_t query;
    char *str;
    char *uri_string = "mongodb://localhost:27017";
    mongoc_uri_t *uri;
    bson_error_t error;
    mongoc_init ();
    uri = mongoc_uri_new_with_error (uri_string, &error);
    if (!uri) {
        fprintf (stderr, "failed to parse URI: %s\n", error.message);
        return EXIT_FAILURE;
    }
    client = mongoc_client_new_from_uri (uri);
    if (!client) {
        return EXIT_FAILURE;
    }
    collection = mongoc_client_get_collection (client, "testdb", "testcollection");
    bson_init (&query);
    cursor = mongoc_collection_find_with_opts (collection, &query, NULL, NULL);
    while (mongoc_cursor_next (cursor, &doc)) {
        str = bson_as_json (doc, NULL);
        printf ("%s\n", str);
        bson_free (str);
    }
    bson_destroy (&query);
    mongoc_cursor_destroy (cursor);
    mongoc_collection_destroy (collection);
    mongoc_uri_destroy (uri);
    mongoc_client_destroy (client);
    mongoc_cleanup ();
    return EXIT_SUCCESS;
}

The above code creates a client object and connects to the local MongoDB instance on port 27017. Note that you need to replace the testdb and the testcollection with the names of your own database and collection accordingly.

Displaying the Available Databases

With the connection established, we can now display the available databases. We use the mongoc_client_get_database_names function to retrieve an array of database names. Here's the code:

#include <stdio.h>
#include <bson/bson.h>
#include <mongoc/mongoc.h>

int main() {
    mongoc_client_t *client;
    mongoc_database_t *database;
    char **database_names;
    bson_error_t error;
    char *uri_string = "mongodb://localhost:27017";
    mongoc_uri_t *uri;
    int i;
    mongoc_init ();
    uri = mongoc_uri_new_with_error (uri_string, &error);
    if (!uri) {
        fprintf (stderr, "failed to parse URI: %s\n", error.message);
        return EXIT_FAILURE;
    }
    client = mongoc_client_new_from_uri (uri);
    if (!client) {
        return EXIT_FAILURE;
    }
    database_names = mongoc_client_get_database_names (client, &error);
    if (!database_names) {
        fprintf (stderr, "%s\n", error.message);
        return EXIT_FAILURE;
    }
    for (i = 0; database_names[i]; i++) {
        printf ("%s\n", database_names[i]);
    }
    bson_strfreev (database_names);
    mongoc_uri_destroy (uri);
    mongoc_client_destroy (client);
    mongoc_cleanup ();
    return EXIT_SUCCESS;
}

The above code retrieves an array of database names using the mongoc_client_get_database_names function and displays them to the console. Note that we need to free the database_names array afterward using the bson_strfreev function.

Conclusion

We have shown you how to use C Programming Language to connect to a MongoDB instance and display its available databases. The libmongoc C driver provides a flexible and efficient way to interact with MongoDB, making it a popular choice for C programmers.