문서 메뉴
문서 홈
/ / /
C 드라이버
/ /

컬렉션 인덱스 관리

MongoDB 컬렉션에 인덱스를 만들려면 mongoc_collection_create_indexes_with_opts를 사용합니다.

// `keys` represents an ascending index on field `x`.
bson_t *keys = BCON_NEW ("x", 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 ("Successfully created index\n");
} else {
bson_destroy (keys);
HANDLE_ERROR ("Failed to create index: %s", error.message);
}
bson_destroy (keys);

인덱스를 나열하려면 mongoc_collection_find_indexes_with_opts를 사용합니다.

mongoc_cursor_t *cursor = mongoc_collection_find_indexes_with_opts (coll, NULL /* opts */);
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)) {
mongoc_cursor_destroy (cursor);
HANDLE_ERROR ("Failed to list indexes: %s", error.message);
}
mongoc_cursor_destroy (cursor);

인덱스를 삭제하려면 mongoc_collection_drop_index_with_opts 를 사용합니다. . 인덱스 이름은 keys mongoc_collection_keys_to_index_string 을사용하여 문서에서 가져올 수 있습니다.

bson_t *keys = BCON_NEW ("x", 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 ("Successfully dropped index\n");
} else {
bson_free (index_name);
bson_destroy (keys);
HANDLE_ERROR ("Failed to drop index: %s", error.message);
}
bson_free (index_name);
bson_destroy (keys);

전체 예시는 example-manage-collection-indexes.c를 참조하세요.

Atlas Search 인덱스를 만들려면 createSearchIndexes 명령을 사용합니다.

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);

Atlas Search 인덱스를 나열하려면 $listSearchIndexes 애그리게이션 단계를 사용합니다.

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);

Atlas Search 인덱스를 업데이트하려면 updateSearchIndex 명령을 사용합니다.

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);

Atlas Search 인덱스를 삭제하려면 dropSearchIndex 명령을 사용합니다.

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);

전체 예시는 example-manage-search-indexes.c를 참조하세요.

돌아가기

Microsoft Visual Studio 프로젝트에서 libmongoc 사용

다음

디버깅 보조 도구