MONGOC_CLIENT...R_DESCRIPTIONS(3)

πŸ’… MONGOC_CLIENT...R_DESCRIPTIONS(3) πŸ’…

okay bestie, welcome to libmongoc β€” this is the page for mongoc_client_get_server_descriptions and it's honestly such a main character function. it's giving "let me introduce you to everyone in the group chat" energy, except the group chat is your MongoDB topology. πŸ¦‹


✨ SYNOPSIS ✨

     mongoc_server_description_t **
     mongoc_client_get_server_descriptions (const mongoc_client_t *client,
                                            size_t *n);

Fetches an array of mongoc_server_description_t structs for all known servers in the topology β€” like, every single one, no gatekeeping. πŸ™ˆ

Some receipts you need to know, no cap:

  • it returns no servers until the client connects β€” so don't come for her before she's even linked up πŸ’€
  • it returns a single server if the client is directly connected
  • it returns all members of a replica set</> if the client's MongoDB URI includes a "replicaSet" option
  • it returns all known mongos servers if the MongoDB URI includes a list of them

and here's the cutest little example, show_servers, serving full tutorial realness πŸŽ€:

     void
     show_servers (const mongoc_client_t *client)
     {
        bson_t *b = BCON_NEW ("ping", BCON_INT32 (1));
        bson_error_t error;
        bool r;
        mongoc_server_description_t **sds;
        size_t i, n;

        /* ensure client has connected */
        r = mongoc_client_command_simple (client, "db", b, NULL, NULL, &error);
        if (!r) {
           MONGOC_ERROR ("could not connect: %s\n", error.message);
           return;
        }

        sds = mongoc_client_get_server_descriptions (client, &n);

        for (i = 0; i < n; ++i) {
           printf ("%s\n", mongoc_server_description_host (sds[i])->host_and_port);
        }

        mongoc_server_descriptions_destroy_all (sds, n);
        bson_destroy (&b);
     }

🌸 PARAMETERS 🌸

  • client β€” a mongoc_client_t. the client itself. the moment. the whole vibe. πŸ‘‘
  • n β€” receives the length of the descriptions array. so she writes the count back out to you, bestie, you don't gotta count by hand πŸ’–

πŸ¦‹ RETURNS πŸ¦‹

A newly allocated array β€” and yes, newly allocated means you own it now, it's your responsibility, no ghosting allowed 😭. You must free it with mongoc_server_descriptions_destroy_all().

fr fr do not skip the free. that's a memory leak and it's not iconic, it's just sad. πŸ’€

⭐ AUTHOR ⭐

MongoDB, Inc β€” the ones who made all this happen, periodt. 🫢


2009-present, MongoDB, Inc.


okay that's the whole page, you ate that reading it tbh πŸ’…βœ¨

~ cute little sign-off ~ this iconic dispatch came to you from libmongoc, version 2.5.6, dated Oct 09, 2026. go forth and fetch those server descriptions, bestie β€” slay responsibly 🌈 πŸ“

πŸ“¦ where this page comes from