개요
이 페이지에서는 C 운전자 사용하여 다양한 유형의 인덱스를 관리 방법을 보여주는 복사 가능한 코드 예제를 볼 수 있습니다.
팁
인덱스 작업에 학습 보려면 인덱스 작업 가이드 를 참조하세요. 이 페이지에 표시된 인덱스에 학습 보려면 각 섹션에 제공된 링크를 참조하세요.
이 페이지의 예제를 사용하려면 코드 예제를 샘플 애플리케이션 또는 자체 애플리케이션에 복사합니다. 코드 예제의 모든 자리 표시자(예: <connection string URI>)를 MongoDB 배포에 필요한 관련 값으로 바꿔야 합니다.
샘플 애플리케이션
다음 샘플 애플리케이션을 사용하여 이 페이지의 코드 예제를 테스트할 수 있습니다. 샘플 애플리케이션을 사용하려면 다음 단계를 수행하세요.
C 운전자 설치되어 있는지 확인합니다.
다음 코드를 복사하여 새
.c파일에 붙여넣습니다.이 페이지에서 코드 예제를 복사하여 파일의 지정된 줄에 붙여넣습니다.
1 2 3 4 5 int 6 main (void) 7 { 8 mongoc_client_t *client; 9 mongoc_collection_t *collection; 10 bson_error_t error; 11 12 mongoc_init (); 13 14 client = mongoc_client_new ("<connection string URI>"); 15 collection = mongoc_client_get_collection (client, "<database name>", "collection name"); 16 17 // Start example code here 18 19 // End example code here 20 21 mongoc_collection_destroy (collection); 22 mongoc_client_destroy (client); 23 mongoc_cleanup (); 24 25 return EXIT_SUCCESS; 26 }
단일 필드 인덱스
다음 예시 에서는 지정된 필드 에 오름차순 인덱스 를 생성합니다.
bson_t *keys = BCON_NEW ("<field name>", BCON_INT32 (1)); mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL); if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) { printf ("Successfully created index\n"); } else { fprintf (stderr, "Failed to create index: %s", error.message); } bson_destroy (keys); mongoc_index_model_destroy (index_model);
단일 필드 인덱스에 대해 자세히 알아보려면 단일 필드 인덱스 가이드를 참조하세요.
복합 인덱스
다음 예시 에서는 지정된 필드에 두 개의 오름차순 인덱스로 구성된 복합 인덱스 를 만듭니다.
bson_t *keys = BCON_NEW ("<field name 1>", BCON_INT32 (1), "<field name 2>", BCON_INT32 (1)); mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL); if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) { printf ("Successfully created index\n"); } else { fprintf (stderr, "Failed to create index: %s", error.message); } bson_destroy (keys); mongoc_index_model_destroy (index_model);
복합 인덱스에 대해 자세히 알아보려면 복합 인덱스 가이드를 참조하세요.
Multikey Index
다음 예시 에서는 지정된 배열 값 필드 에 오름차순 멀티키 인덱스 를 생성합니다.
bson_t *keys = BCON_NEW ("<array field name>", BCON_INT32 (1)); mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL); if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) { printf ("Successfully created index\n"); } else { fprintf (stderr, "Failed to create index: %s", error.message); } bson_destroy (keys); mongoc_index_model_destroy (index_model);
멀티키 인덱스에 대해 자세히 알아보려면 멀티키 인덱스 가이드를 참조하세요.
지리 공간적 인덱스
다음 예시 에서는 GeoJSON 객체를 포함하는 지정된 필드 에 2dsphere 인덱스 를 생성합니다.
bson_t *keys = BCON_NEW ("<GeoJSON object field name>", BCON_UTF8 ("2dsphere")); mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL); if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) { printf ("Successfully created index\n"); } else { fprintf (stderr, "Failed to create index: %s", error.message); } bson_destroy (keys); mongoc_index_model_destroy (index_model);
GeoJSON 데이터 유형 에 학습 보려면 MongoDB Server 매뉴얼에서 GeoJSON 객체 를 참조하세요.
고유 인덱스
다음 예시 에서는 지정된 필드 에 오름차순 고유 인덱스 를 생성합니다.
bson_t *keys = BCON_NEW ("title", BCON_INT32 (1)); bson_t *opts = BCON_NEW ("unique", BCON_BOOL (true)); mongoc_index_model_t *index_model = mongoc_index_model_new (keys, opts); if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) { printf ("Successfully created index\n"); } else { fprintf (stderr, "Failed to create index: %s", error.message); } bson_destroy (keys); bson_destroy (opts); mongoc_index_model_destroy (index_model);
와일드카드 인덱스
다음 예시 지정된 컬렉션 에 오름차순 와일드카드 인덱스 생성합니다.
bson_t *keys = BCON_NEW ("$**", BCON_INT32 (1)); mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL); if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) { printf ("Successfully created index\n"); } else { fprintf (stderr, "Failed to create index: %s", error.message); } bson_destroy (keys); mongoc_index_model_destroy (index_model);
클러스터된 인덱스
다음 예시 _id 필드 에 오름차순 클러스터형 인덱스 사용하여 새 컬렉션 을 만듭니다.
bson_t *opts = BCON_NEW ("clusteredIndex", "{", "key", "{", "_id", BCON_INT32 (1), "}", "unique", BCON_BOOL (true), "}"); mongoc_database_t *database = mongoc_client_get_database (client, "<database name>"); if (mongoc_database_create_collection (database, "<collection name>", opts, &error)) { printf ("Successfully created collection\n"); } else { fprintf (stderr, "Failed to create collection: %s", error.message); } mongoc_database_destroy (database); bson_destroy (opts);
Text Index
다음 예시 에서는 지정된 string 필드 에 텍스트 인덱스 를 생성합니다.
bson_t *keys = BCON_NEW ("<field name>", BCON_UTF8 ("text")); mongoc_index_model_t *index_model = mongoc_index_model_new (keys, NULL); if (mongoc_collection_create_indexes_with_opts (collection, &index_model, 1, NULL, NULL, &error)) { printf ("Successfully created index\n"); } else { fprintf (stderr, "Failed to create index: %s", error.message); } bson_destroy (keys); mongoc_index_model_destroy (index_model);
인덱스 삭제
다음 예시 에서는 지정된 이름의 인덱스 를 삭제합니다.
if (mongoc_collection_drop_index (collection, "<index name>", &error)) { printf ("Successfully dropped index\n"); } else { fprintf (stderr, "Failed to drop index: %s", error.message); }
인덱스 제거에 대해 자세히 알아보려면 인덱스 작업 가이드에서 인덱스 제거 를 참조하세요.
MongoDB Search 인덱스 관리
다음 섹션에는 MongoDB Search 인덱스를 관리 방법을 설명하는 코드 예제가 포함되어 있습니다.
MongoDB Search 인덱스에 대해 자세히 학습 MongoDB Search 인덱스 가이드 참조하세요.
Atlas Search 인덱스 만들기
다음 예시 지정된 필드 에 MongoDB Search 인덱스 생성합니다.
bson_t cmd; char *cmd_str = bson_strdup_printf ( BSON_STR ({ "createSearchIndexes" : "%s", "indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<index name>"} ] }), "<collection name>"); bson_init_from_json (&cmd, cmd_str, -1, &error); bson_free (cmd_str); if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) { printf ("Successfully created search index\n"); } else { fprintf (stderr, "Failed to create search index: %s", error.message); } bson_destroy (&cmd);
검색 인덱스 나열
다음 예시 에서는 지정된 컬렉션 의 MongoDB Search 인덱스 목록을 출력합니다.
bson_t pipeline; const bson_t *doc; const char *pipeline_str = BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]}); bson_init_from_json (&pipeline, pipeline_str, -1, &error); mongoc_cursor_t *cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL); while (mongoc_cursor_next (cursor, &doc)) { char *str = bson_as_canonical_extended_json (doc, NULL); printf ("%s\n", str); bson_free (str); } bson_destroy (&pipeline); mongoc_cursor_destroy (cursor);
Atlas Search 인덱스 나열에 대해 자세히 알아보려면 Atlas Search 인덱스 나열 가이드를 참조하세요.
검색 인덱스 업데이트
다음 예시 지정된 새 인덱스 정의로 기존 MongoDB Search 인덱스 업데이트합니다.
bson_t cmd; char *cmd_str = bson_strdup_printf ( BSON_STR ({ "updateSearchIndex" : "%s", "definition" : {"mappings" : {"dynamic" : true}}, "name" : "<index name>"}), "<collection name>"); bson_init_from_json (&cmd, cmd_str, -1, &error); bson_free (cmd_str); if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) { printf ("Successfully updated search index\n"); } else { fprintf (stderr, "Failed to create search index: %s", error.message); } bson_destroy (&cmd);
Atlas Search 인덱스 업데이트에 대해 자세히 알아보려면 Atlas Search 인덱스 업데이트 가이드를 참조하세요.
Atlas Search 인덱스 삭제
다음 예시 지정된 이름의 MongoDB Search 인덱스 삭제합니다.
bson_t cmd; char *cmd_str = bson_strdup_printf ( BSON_STR ({ "dropSearchIndexes" : "%s", "index" : "<index name>" }), "<collection name>"); bson_init_from_json (&cmd, cmd_str, -1, &error); if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) { printf ("Successfully deleted search index\n"); } else { fprintf (stderr, "Failed to delete search index: %s", error.message); } bson_destroy (&cmd);
Atlas Search 인덱스 삭제에 대해 자세히 알아보려면 Atlas Search 인덱스 삭제 가이드를 참조하세요.