Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Docs Menu
Docs Home
/ / /
C++ 드라이버

인덱스로 쿼리 최적화

이 페이지의 내용

  • 개요
  • 샘플 애플리케이션
  • 단일 필드 인덱스
  • 복합 인덱스
  • 인덱스 제거
  • 모든 인덱스 제거
  • Atlas 검색 인덱스 관리
  • Atlas Search 인덱스 만들기
  • 검색 인덱스 나열
  • 검색 인덱스 업데이트
  • Atlas Search 인덱스 삭제

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

인덱스 작업에 학습 보려면 인덱스 작업 가이드 를 참조하세요. 이 페이지에 표시된 인덱스에 학습 보려면 각 섹션에 제공된 링크를 참조하세요.

이 페이지의 예제를 사용하려면 코드 예제를 샘플 애플리케이션 또는 자체 애플리케이션에 복사합니다. 코드 예제의 모든 자리 표시자(예: <connection string>)를 MongoDB 배포에 필요한 관련 값으로 바꿔야 합니다.

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

  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;
Index created: { "name" : "fieldName_1" }

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

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

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;
Index created: { "name" : "fieldName1_-1_fieldName2_-1" }

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

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

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

인덱스 제거에 학습 보려면 인덱스 작업 가이드 의 인덱스 제거 섹션을 참조하세요.

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

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

인덱스 제거에 학습 보려면 인덱스 작업 가이드 의 인덱스 제거 섹션을 참조하세요.

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

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

// 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;
New index name: searchIndexName

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

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

다음 코드는 지정된 새 인덱스 정의로 기존 Atlas Search 인덱스 업데이트합니다.

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

다음 코드는 지정된 이름의 Atlas Search 인덱스 삭제합니다.

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

돌아가기

데이터베이스 & collection