개요
Single field indexes are indexes that hold a reference to a single field in a collection's documents. These indexes improve single field query and sort performance. They also support TTL Indexes that automatically remove documents from a collection after a certain amount of time or at a specified clock time.
단일 필드 인덱스 를 만들려면 create_index() 메서드를 호출하고 다음 정보가 포함된 문서 를 지정합니다.
인덱스 를 생성할 필드입니다.
인덱싱된 값의 정렬 순서입니다. 오름차순에는
1을 사용하고 내림차순에는-1을 사용합니다.
참고
기본값 _id_ 인덱스 는 단일 필드 인덱스 의 예시 입니다. 이 인덱스 는 새 컬렉션 이 생성될 때 _id 필드 에 자동으로 생성됩니다.
샘플 데이터
The examples in this guide use the movies collection in the sample_mflix database from the Atlas sample datasets. To access this collection from your C++ application, instantiate a mongocxx::client that connects to an Atlas cluster and assign the following values to your db and collection variables:
auto db = client["sample_mflix"]; auto collection = db["movies"];
무료 MongoDB Atlas 클러스터 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면 MongoDB 시작하기 가이드 를 참조하세요.
단일 필드 인덱스 만들기
다음 예시 에서는 create_index() 메서드를 사용하여 title 필드 에 오름차순 단일 필드 인덱스 를 생성합니다.
auto index_specification = make_document(kvp("title", 1)); auto result = collection.create_index(index_specification.view());
다음 쿼리 는 앞의 코드 예시 에서 생성된 인덱스 에 포함됩니다.
auto document = collection.find_one(make_document(kvp("title","Peter Pan"))); std::cout << bsoncxx::to_json(*document) << std::endl;
추가 정보
인덱스 관리 방법을 보여주는 실행 가능한 예제를 보려면 인덱스를 사용하여 쿼리 최적화를 참조하세요.
인덱스에 학습 보려면 MongoDB Server 매뉴얼에서 다음 리소스를 참조하세요.
API 문서
이 가이드 에서 설명하는 메서드에 대해 학습 보려면 다음 API 설명서를 참조하세요.