AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

인덱스로 쿼리 최적화

이 페이지에서는 C++ 운전자 를 사용하여 일반적인 유형의 인덱스로 작업하는 방법을 보여주는 복사 가능한 코드 예제를 볼 수 있습니다.

팁

이 페이지에 표시된 인덱스에 대해 자세히 학습 각 섹션에 제공된 링크를 참조하세요.

To use an example from this page, copy the code example into the sample application or your own application. Be sure to replace all placeholders in the code examples, such as <connection string>, with the relevant values for your MongoDB deployment.

다음 샘플 애플리케이션을 사용하여 이 페이지의 코드 예제를 테스트할 수 있습니다. 샘플 애플리케이션을 사용하려면 다음 단계를 수행하세요.

  1. 프로젝트 에서 가져올 수 있는 위치 에 C++ 운전자 가 설치되어 있는지 확인합니다.

  2. 다음 코드를 복사하여 프로젝트 내의 새 .cpp 파일 에 붙여넣습니다.

  3. 이 페이지에서 코드 예시 를 복사하여 파일 의 강조 표시된 섹션에 붙여넣습니다.

1#include <iostream>
2
3#include <bsoncxx/builder/basic/document.hpp>
4#include <bsoncxx/json.hpp>
5#include <mongocxx/client.hpp>
6#include <mongocxx/exception/exception.hpp>
7#include <mongocxx/instance.hpp>
8#include <mongocxx/uri.hpp>
9
10using bsoncxx::builder::basic::kvp;
11using bsoncxx::builder::basic::make_document;
12
13int main() {
14 try {
15 mongocxx::instance instance;
16
17 mongocxx::uri uri("<connection string>");
18 mongocxx::client client(uri);
19
20 auto database = client["<database name>"];
21 auto collection = database["<collection name>"];
22
23 // Start example code here
24
25 // End example code here
26
27 } catch (const mongocxx::exception& e) {
28 std::cout << "An exception occurred: " << e.what() << "\n";
29 return EXIT_FAILURE;
30 }
31
32 return EXIT_SUCCESS;
33}

다음 코드는 오름차순 단일 필드 인덱스 를 만드는 방법을 보여줍니다.

auto index_specification = make_document(kvp("<fieldName>", 1));
auto result = collection.create_index(index_specification.view());
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;

단일 필드 인덱스에 학습 보려면 단일 필드 인덱스 가이드 를 참조하세요.

다음 코드는 내림차순 복합 인덱스 를 생성하는 방법을 보여줍니다.

auto index_specification = make_document(kvp("<fieldName1>", -1), kvp("<fieldName2>", -1));
auto result = collection.create_index(index_specification.view());
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;

복합 인덱스에 대해 자세히 알아보려면 복합 인덱스 가이드를 참조하세요.

다음 코드는 인덱스 를 제거 하는 방법을 보여줍니다.

collection.indexes().drop_one("<indexName>");
std::cout << "Index dropped." << std::endl;

다음 코드는 컬렉션 의 모든 인덱스를 제거 하는 방법을 보여줍니다.

collection.indexes().drop_all();
std::cout << "All indexes removed." << std::endl;

다음 섹션에는 MongoDB Search 검색 인덱스를 관리하는 방법을 설명하는 코드 예시가 포함되어 있습니다. MongoDB Search 인덱스에 대해 자세히 학습하려면 MongoDB Search 인덱스 가이드 를 참조하세요.

다음 코드는 지정된 컬렉션 에서 지원되는 모든 필드를 동적으로 인덱싱하는 MongoDB 검색 인덱스 생성하는 방법을 보여줍니다.

// Create an index model with your index name and definition
auto siv = collection.search_indexes();
auto name = "<searchIndexName>";
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", true))));
auto model = mongocxx::search_index_model(name, definition.view());
// Create the search index
auto result = siv.create_one(model);
std::cout << "New index name: " << result << std::endl;

다음 코드는 지정된 컬렉션의 MongoDB 검색 인덱스 목록을 출력합니다.

auto siv = collection.search_indexes();
auto result = siv.list();
for (const auto &idx : result) {
std::cout << bsoncxx::to_json(idx) << std::endl;
}

다음 코드는 지정된 새 인덱스 정의로 기존 MongoDB 검색 인덱스를 업데이트합니다.

auto siv = collection.search_indexes();
auto update_fields = make_document(kvp("<fieldName>", make_document(kvp("type", "<fieldType>"))));
auto update_definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", update_fields))));
siv.update_one("<searchIndexName>", update_definition.view());

다음 코드는 지정된 이름의 MongoDB 검색 인덱스를 삭제합니다.

auto siv = collection.search_indexes();
siv.drop_one("<searchIndexName>");