Provided by: libmongoc-doc_1.26.0-1.1ubuntu2_all bug

MANAGE ATLAS SEARCH INDEXES

       To create an Atlas Search Index, use the createSearchIndexes command:

          bson_t cmd;
          // Create command.
          {
             char *cmd_str = bson_strdup_printf (
                BSON_STR ({
                   "createSearchIndexes" : "%s",
                   "indexes" : [ {
                      "definition" : {"mappings" : {"dynamic" : false}},
                      "name" : "test-index"
                   } ]
                }),
                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 ("Failed to run createSearchIndexes: %s", error.message);
          }
          printf ("Created index: \"test-index\"\n");
          bson_destroy (&cmd);

       To list Atlas Search Indexes, use the $listSearchIndexes aggregation stage:

          const char *pipeline_str =
             BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
          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 ("Listing indexes:\n");
          const bson_t *got;
          while (mongoc_cursor_next (cursor, &got)) {
             char *got_str = bson_as_canonical_extended_json (got, NULL);
             printf ("  %s\n", got_str);
             bson_free (got_str);
          }
          if (mongoc_cursor_error (cursor, &error)) {
             bson_destroy (&pipeline);
             mongoc_cursor_destroy (cursor);
             HANDLE_ERROR ("Failed to run $listSearchIndexes: %s", error.message);
          }
          bson_destroy (&pipeline);
          mongoc_cursor_destroy (cursor);

       To update an Atlas Search Index, use the updateSearchIndex command:

          bson_t cmd;
          // Create command.
          {
             char *cmd_str = bson_strdup_printf (
                BSON_STR ({
                   "updateSearchIndex" : "%s",
                   "definition" : {"mappings" : {"dynamic" : true}},
                   "name" : "test-index"
                }),
                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 ("Failed to run updateSearchIndex: %s", error.message);
          }
          printf ("Updated index: \"test-index\"\n");
          bson_destroy (&cmd);

       To drop an Atlas Search Index, use the dropSearchIndex command:

          bson_t cmd;
          // Create command.
          {
             char *cmd_str = bson_strdup_printf (
                BSON_STR ({"dropSearchIndex" : "%s", "name" : "test-index"}),
                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 ("Failed to run dropSearchIndex: %s", error.message);
          }
          printf ("Dropped index: \"test-index\"\n");
          bson_destroy (&cmd);

       For a full example, see example-manage-search-indexes.c.

AUTHOR

       MongoDB, Inc

COPYRIGHT

       2017-present, MongoDB, Inc