.\" 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_MANAGE_COLLECTION_INDEXES" "3" "May 07, 2024" "1.27.1" "libmongoc" .sp To create indexes on a MongoDB collection, use \fI\%mongoc_collection_create_indexes_with_opts()\fP: .INDENT 0.0 .INDENT 3.5 .sp .EX // \(gakeys\(ga represents an ascending index on field \(gax\(ga. bson_t *keys = BCON_NEW (\(dqx\(dq, BCON_INT32 (1)); mongoc_index_model_t *im = mongoc_index_model_new (keys, NULL /* opts */); if (mongoc_collection_create_indexes_with_opts (coll, &im, 1, NULL /* opts */, NULL /* reply */, &error)) { printf (\(dqSuccessfully created index\en\(dq); } else { bson_destroy (keys); HANDLE_ERROR (\(dqFailed to create index: %s\(dq, error.message); } bson_destroy (keys); .EE .UNINDENT .UNINDENT .sp To list indexes, use \fI\%mongoc_collection_find_indexes_with_opts()\fP: .INDENT 0.0 .INDENT 3.5 .sp .EX mongoc_cursor_t *cursor = mongoc_collection_find_indexes_with_opts (coll, NULL /* opts */); printf (\(dqListing indexes:\en\(dq); const bson_t *got; while (mongoc_cursor_next (cursor, &got)) { char *got_str = bson_as_canonical_extended_json (got, NULL); printf (\(dq %s\en\(dq, got_str); bson_free (got_str); } if (mongoc_cursor_error (cursor, &error)) { mongoc_cursor_destroy (cursor); HANDLE_ERROR (\(dqFailed to list indexes: %s\(dq, error.message); } mongoc_cursor_destroy (cursor); .EE .UNINDENT .UNINDENT .sp To drop an index, use \fI\%mongoc_collection_drop_index_with_opts()\fP\&. The index name may be obtained from the \fBkeys\fP document with \fI\%mongoc_collection_keys_to_index_string()\fP: .INDENT 0.0 .INDENT 3.5 .sp .EX bson_t *keys = BCON_NEW (\(dqx\(dq, BCON_INT32 (1)); char *index_name = mongoc_collection_keys_to_index_string (keys); if (mongoc_collection_drop_index_with_opts (coll, index_name, NULL /* opts */, &error)) { printf (\(dqSuccessfully dropped index\en\(dq); } else { bson_free (index_name); bson_destroy (keys); HANDLE_ERROR (\(dqFailed to drop index: %s\(dq, error.message); } bson_free (index_name); bson_destroy (keys); .EE .UNINDENT .UNINDENT .sp For a full example, see \fI\%example\-manage\-collection\-indexes.c\fP\&. .SH MANAGE ATLAS SEARCH INDEXES .sp To create an Atlas Search Index, use the \fBcreateSearchIndexes\fP command: .INDENT 0.0 .INDENT 3.5 .sp .EX bson_t cmd; // Create command. { char *cmd_str = bson_strdup_printf ( BSON_STR ({ \(dqcreateSearchIndexes\(dq : \(dq%s\(dq, \(dqindexes\(dq : [ {\(dqdefinition\(dq : {\(dqmappings\(dq : {\(dqdynamic\(dq : false}}, \(dqname\(dq : \(dqtest\-index\(dq} ] }), collname); ASSERT (bson_init_from_json (&cmd, cmd_str, \-1, &error)); bson_free (cmd_str); } if (!mongoc_collection_command_simple (coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) { bson_destroy (&cmd); HANDLE_ERROR (\(dqFailed to run createSearchIndexes: %s\(dq, error.message); } printf (\(dqCreated index: \e\(dqtest\-index\e\(dq\en\(dq); bson_destroy (&cmd); .EE .UNINDENT .UNINDENT .sp To list Atlas Search Indexes, use the \fB$listSearchIndexes\fP aggregation stage: .INDENT 0.0 .INDENT 3.5 .sp .EX const char *pipeline_str = BSON_STR ({\(dqpipeline\(dq : [ {\(dq$listSearchIndexes\(dq : {}} ]}); bson_t pipeline; ASSERT (bson_init_from_json (&pipeline, pipeline_str, \-1, &error)); mongoc_cursor_t *cursor = mongoc_collection_aggregate (coll, MONGOC_QUERY_NONE, &pipeline, NULL /* opts */, NULL /* read_prefs */); printf (\(dqListing indexes:\en\(dq); const bson_t *got; while (mongoc_cursor_next (cursor, &got)) { char *got_str = bson_as_canonical_extended_json (got, NULL); printf (\(dq %s\en\(dq, got_str); bson_free (got_str); } if (mongoc_cursor_error (cursor, &error)) { bson_destroy (&pipeline); mongoc_cursor_destroy (cursor); HANDLE_ERROR (\(dqFailed to run $listSearchIndexes: %s\(dq, error.message); } bson_destroy (&pipeline); mongoc_cursor_destroy (cursor); .EE .UNINDENT .UNINDENT .sp To update an Atlas Search Index, use the \fBupdateSearchIndex\fP command: .INDENT 0.0 .INDENT 3.5 .sp .EX bson_t cmd; // Create command. { char *cmd_str = bson_strdup_printf ( BSON_STR ( {\(dqupdateSearchIndex\(dq : \(dq%s\(dq, \(dqdefinition\(dq : {\(dqmappings\(dq : {\(dqdynamic\(dq : true}}, \(dqname\(dq : \(dqtest\-index\(dq}), collname); ASSERT (bson_init_from_json (&cmd, cmd_str, \-1, &error)); bson_free (cmd_str); } if (!mongoc_collection_command_simple (coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) { bson_destroy (&cmd); HANDLE_ERROR (\(dqFailed to run updateSearchIndex: %s\(dq, error.message); } printf (\(dqUpdated index: \e\(dqtest\-index\e\(dq\en\(dq); bson_destroy (&cmd); .EE .UNINDENT .UNINDENT .sp To drop an Atlas Search Index, use the \fBdropSearchIndex\fP command: .INDENT 0.0 .INDENT 3.5 .sp .EX bson_t cmd; // Create command. { char *cmd_str = bson_strdup_printf (BSON_STR ({\(dqdropSearchIndex\(dq : \(dq%s\(dq, \(dqname\(dq : \(dqtest\-index\(dq}), collname); ASSERT (bson_init_from_json (&cmd, cmd_str, \-1, &error)); bson_free (cmd_str); } if (!mongoc_collection_command_simple (coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) { bson_destroy (&cmd); HANDLE_ERROR (\(dqFailed to run dropSearchIndex: %s\(dq, error.message); } printf (\(dqDropped index: \e\(dqtest\-index\e\(dq\en\(dq); bson_destroy (&cmd); .EE .UNINDENT .UNINDENT .sp For a full example, see \fI\%example\-manage\-search\-indexes.c\fP\&. .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .