cmongodb

Why is the demo program for MongoDB throwing a warning before exiting?


I am following the tutorial for MongoDB's C driver. This is the demo (hello database) program:

#include <mongoc/mongoc.h>

int main(void) {
    mongoc_client_t *client = NULL;
    bson_error_t error = {0};
    mongoc_server_api_t *api = NULL;
    mongoc_database_t *database = NULL;
    bson_t *command = NULL;
    bson_t reply = BSON_INITIALIZER;
    int rc = 0;
    bool ok = true;

    // Initialize the MongoDB C Driver.
    mongoc_init();

    client = mongoc_client_new("<connection string>");
    if (!client) {
        fprintf(stderr, "Failed to create a MongoDB client.\n");
        rc = 1;
        goto cleanup;
    }

    // Set the version of the Stable API on the client.
    api = mongoc_server_api_new(MONGOC_SERVER_API_V1);
    if (!api) {
        fprintf(stderr, "Failed to create a MongoDB server API.\n");
        rc = 1;
        goto cleanup;
    }

    ok = mongoc_client_set_server_api(client, api, &error);
    if (!ok) {
        fprintf(stderr, "error: %s\n", error.message);
        rc = 1;
        goto cleanup;
    }

    // Get a handle on the "admin" database.
    database = mongoc_client_get_database(client, "sample_restaurants"); // does not matter what database I use
    if (!database) {
        fprintf(stderr, "Failed to get a MongoDB database handle.\n");
        rc = 1;
        goto cleanup;
    }

    // Ping the database.
    command = BCON_NEW("ping", BCON_INT32(1));
    ok = mongoc_database_command_simple(
        database, command, NULL, &reply, &error
    );
    if (!ok) {
        fprintf(stderr, "error: %s\n", error.message);
        rc = 1;
        goto cleanup;
    }
    bson_destroy(&reply);

    printf("Pinged your deployment. You successfully connected to MongoDB!\n");

// Perform cleanup.
cleanup:
    bson_destroy(command);
    mongoc_database_destroy(database);
    mongoc_server_api_destroy(api);
    mongoc_client_destroy(client);
    mongoc_cleanup();

    return rc;
}

The output I get:

Pinged your deployment. You successfully connected to MongoDB!
2025/07/08 02:21:14.0310: [ 4981]:  WARNING:       client: Couldn't send "endSessions": (Unauthorized) not authorized on admin to execute command { endSessions: [[{id {4 [28 203 190 94 175 45 73 123 170 86 143 92 173 213 219 217]}}]], $db: "admin", $clusterTime: { clusterTime: {1751955674 24}, signature: { hash: {0 [63 11 33 56 229 121 251 43 192 100 254 6 0 186 230 195 215 177 133 49]}, keyId: 7461260273723113472.000000 } }, apiVersion: "1" }

Why does it exit with a warning even though it seems to have run successfully?


Solution

  • So If you have control over the MongoDB user roles, you can add the endSessions privilege on the admin database. This requires adding the clusterManager or a custom role with endSessions permission.

    Or Since it does not affect your program's functionality, you can just safely ignore this warning in development or simple demos.

    But if it is for production or more advanced usage, use a user with sufficient privileges to avoid such warnings.