MONGOC_CREATE_INDEXES(3)
β¨ 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: thecommitQuorumoption to thecreateIndexescommand 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 passcommitQuorumif you're connected to server versions less than 4.4. Using thecommitQuorumoption 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 π
π¦ COPYRIGHT π¦
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
- debian bookworm: libmongoc-doc 1.23.1-1+deb12u3 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms