MONGOC_CREATE_INDEXES(3)

πŸ“¦
version 1.23.1 Β· 2 versions
, in debian bookworm

✨ MONGOC_CREATE_INDEXES(3) ✨

okay bestie, welcome to mongoc_create_indexes β€” it's giving "Creating Indexes" πŸ’… and it lives in libmongoc. This is the page for when you wanna build indexes on a MongoDB collection.


πŸŽ€ NAME πŸŽ€

mongoc_create_indexes β€” Creating Indexes ✨

To create indexes on a MongoDB collection, you execute the createIndexes command with a command function, like mongoc_database_write_command_with_opts() or mongoc_collection_write_command_with_opts(). That's the whole vibe. If you want the deep lore, go read the MongoDB Manual entry for the createIndexes command. 🫢

⚠️ WARNING, and I need you to actually read this one bestie: the commitQuorum option to the createIndexes command is only supported in MongoDB 4.4+ servers, but it is not validated in the command functions. So like. The library will not catch you. Do not pass commitQuorum if you're connected to server versions less than 4.4. Using the commitQuorum option on server versions less than 4.4 may have adverse effects on index builds. That's not a vibe, that's a warning. 🚨

πŸ’– EXAMPLE πŸ’–

behold, example-create-indexes.c πŸ‘‘

/* gcc example-create-indexes.c -o example-create-indexes $(pkg-config --cflags
 * --libs libmongoc-1.0) */

/* ./example-create-indexes [CONNECTION_STRING [COLLECTION_NAME]] */

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

int
main (int argc, char *argv[])
{
   mongoc_client_t *client;
   const char *uri_string =
      "mongodb://127.0.0.1/?appname=create-indexes-example";
   mongoc_uri_t *uri;
   mongoc_database_t *db;
   const char *collection_name = "test";
   bson_t keys;
   char *index_name;
   bson_t *create_indexes;
   bson_t reply;
   char *reply_str;
   bson_error_t error;
   bool r;

   mongoc_init ();

   if (argc > 1) {
      uri_string = argv[1];
   }

   if (argc > 2) {
      collection_name = argv[2];
   }

   uri = mongoc_uri_new_with_error (uri_string, &error);
   if (!uri) {
      fprintf (stderr,
               "failed to parse URI: %s\n"
               "error message:       %s\n",
               uri_string,
               error.message);
      return EXIT_FAILURE;
   }

   client = mongoc_client_new_from_uri (uri);
   if (!client) {
      return EXIT_FAILURE;
   }

   mongoc_client_set_error_api (client, 2);
   db = mongoc_client_get_database (client, "test");

   /* ascending index on field "x" */
   bson_init (&keys);
   BSON_APPEND_INT32 (&keys, "x", 1);
   index_name = mongoc_collection_keys_to_index_string (&keys);
   create_indexes = BCON_NEW ("createIndexes",
                              BCON_UTF8 (collection_name),
                              "indexes",
                              "[",
                              "{",
                              "key",
                              BCON_DOCUMENT (&keys),
                              "name",
                              BCON_UTF8 (index_name),
                              "}",
                              "]");

   r = mongoc_database_write_command_with_opts (
      db, create_indexes, NULL /* opts */, &reply, &error);

   reply_str = bson_as_json (&reply, NULL);
   printf ("%s\n", reply_str);

   if (!r) {
      fprintf (stderr, "Error in createIndexes: %s\n", error.message);
   }

   bson_free (index_name);
   bson_free (reply_str);
   bson_destroy (&reply);
   bson_destroy (create_indexes);
   mongoc_database_destroy (db);
   mongoc_uri_destroy (uri);
   mongoc_client_destroy (client);

   mongoc_cleanup ();

   return r ? EXIT_SUCCESS : EXIT_FAILURE;
}

like… the way it just builds an ascending index on field "x" and cleans up after itself? main character energy fr fr. 🌸


🌈 AUTHOR 🌈

MongoDB, Inc β€” iconic, we stan πŸ’œ


2017-present, MongoDB, Inc


and that's the whole page bestie!! πŸŽ€ you were reading libmongoc 1.23.1, dated Apr 27, 2026, and yes, this is MONGOC_CREATE_INDEXES(3) β€” go forth and index responsibly, no cap πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from