.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "MONGOC_CREATE_INDEXES" "3" "Oct 20, 2022" "1.23.1" "libmongoc" .SH NAME mongoc_create_indexes \- Creating Indexes .sp To create indexes on a MongoDB collection, execute the \fBcreateIndexes\fP command with a command function like \fI\%mongoc_database_write_command_with_opts()\fP or \fI\%mongoc_collection_write_command_with_opts()\fP\&. See \fI\%the MongoDB Manual entry for the createIndexes command\fP for details. .sp \fBWARNING:\fP .INDENT 0.0 .INDENT 3.5 The \fBcommitQuorum\fP option to the \fBcreateIndexes\fP command is only supported in MongoDB 4.4+ servers, but it is not validated in the command functions. Do not pass \fBcommitQuorum\fP if connected to server versions less than 4.4. Using the \fBcommitQuorum\fP option on server versions less than 4.4 may have adverse effects on index builds. .UNINDENT .UNINDENT .SH EXAMPLE .sp example\-create\-indexes.c .INDENT 0.0 .INDENT 3.5 .sp .nf .ft 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 #include #include 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\en" "error message: %s\en", 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\en", reply_str); if (!r) { fprintf (stderr, "Error in createIndexes: %s\en", 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; } .ft P .fi .UNINDENT .UNINDENT .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .