Docs Menu
Docs Home
/ / /
C++ 드라이버
/

문서 수 계산

이 페이지의 내용

  • 개요
  • 샘플 데이터
  • 정확한 개수 조회
  • 모든 문서 계산
  • 특정 문서 수 계산
  • 카운트 동작 사용자 지정
  • 예상 개수 조회
  • 예상 카운트 동작 사용자 지정
  • API 문서

이 가이드 에서는 C++ 운전자 를 사용하여 컬렉션 에 있는 문서 수의 정확한 예상 개수를 조회 하는 방법을 학습 수 있습니다. count_documents() 메서드는 쿼리 필터하다 와 일치하거나 컬렉션 에 존재하는 정확한 문서 수를 반환하고, estimated_document_count() 메서드는 컬렉션 의 예상 문서 수를 반환합니다.

이 가이드 의 예제에서는 Atlas 샘플 데이터 세트sample_training 데이터베이스 에 있는 companies 컬렉션 을 사용합니다. C++ 애플리케이션 에서 이 컬렉션 에 액세스 하려면 Atlas cluster 에 연결하는 mongocxx::client 를 인스턴스화하고 dbcollection 변수에 다음 값을 할당합니다.

auto db = client["sample_training"];
auto collection = db["companies"];

무료 MongoDB Atlas cluster 를 생성하고 샘플 데이터 세트를 로드하는 방법을 학습 보려면 Atlas 시작하기 가이드 를 참조하세요.

컬렉션 에 있는 문서 수를 계산하려면 count_documents() 메서드를 사용합니다. 특정 검색 기준과 일치하는 문서 수를 계산하려면 쿼리 필터하다 문서 를 count_documents() 메서드에 전달합니다.

쿼리 지정에 학습 보려면 쿼리 지정 가이드 를 참조하세요.

컬렉션 의 모든 문서 수를 반환하려면 다음 예시 와 같이 빈 필터하다 문서 를 count_documents() 메서드에 전달합니다.

auto result = collection.count_documents({});
std::cout << "Number of documents: " << result << std::endl;
Number of documents: 9500

특정 검색 기준과 일치하는 문서 수를 반환하려면 쿼리 필터하다 문서 를 count_documents() 메서드에 전달합니다.

다음 예시 에서는 founded_year 값이 2010 인 문서 수를 계산합니다.

auto result = collection.count_documents(make_document(kvp("founded_year", 2010)));
std::cout << "Number of companies founded in 2010: " << result << std::endl;
Number of companies founded in 2010: 33

mongocxx::options::count 클래스의 인스턴스 를 매개변수로 전달하여 count_documents() 메서드의 동작을 수정할 수 있습니다. 다음 표에서는 mongocxx::options::count 인스턴스 에서 설정하다 수 있는 필드에 대해 설명합니다.

필드
설명
collation
The collation to use for the operation.
Type: bsoncxx::document::view_or_value
hint
The index to use for the operation.
Type: mongocxx::hint
comment
The comment to attach to the operation.
Type: bsoncxx::types::bson_value::view_or_value
limit
The maximum number of documents to count. This value must be a positive integer.
Type: std::int64_t
max_time
The maximum amount of time in milliseconds that the operation can run.
Type: std::chrono::milliseconds
skip
The number of documents to skip before counting documents.
Type: std::int64_t
read_preference
The read preference to use for the operation.
Type: mongocxx::read_preference

다음 예시 에서는 count_documents() 메서드를 사용하여 number_of_employees 필드 의 값이 50 인 문서 수를 계산하고 최대 100 결과를 계산하도록 작업에 지시합니다.

mongocxx::options::count opts;
opts.limit(100);
auto result = collection.count_documents(make_document(kvp("number_of_employees", 50)), opts);
std::cout << "Number of companies with 50 employees: " << result << std::endl;
Number of companies with 50 employees: 100

estimated_document_count() 메서드를 호출하여 컬렉션 에 있는 문서 수의 추정치를 조회 할 수 있습니다. 이 메서드는 컬렉션 메타데이터 를 기반으로 문서의 양을 추정하며, 이는 정확한 개수를 계산하는 것보다 빠를 수 있습니다.

다음 예에서는 컬렉션의 문서 수를 추정합니다.

auto result = collection.estimated_document_count();
std::cout << "Estimated number of documents: " << result << std::endl;
Estimated number of documents: 9500

mongocxx::options::estimated_document_count 클래스의 인스턴스 를 매개변수로 전달하여 estimated_document_count() 메서드의 동작을 수정할 수 있습니다. 다음 표에서는 mongocxx::options::estimated_document_count 인스턴스 에서 설정하다 수 있는 필드에 대해 설명합니다.

필드
설명
max_time
The maximum amount of time in milliseconds that the operation can run.
Type: std::chrono::milliseconds
comment
The comment to attach to the operation.
Type: bsoncxx::types::bson_value::view_or_value
read_preference
The read preference to use for the operation.
Type: mongocxx::read_preference

다음 예시 에서는 estimated_document_count() 메서드를 사용하여 컬렉션 에 있는 문서 수의 추정치를 반환하고 최대 1000 밀리초 동안 작업을 실행 하도록 지시합니다.

mongocxx::options::estimated_document_count opts;
opts.max_time(std::chrono::milliseconds{1000});
auto result = collection.estimated_document_count(opts);
std::cout << "Estimated number of documents: " << result << std::endl;
Estimated number of documents: 9500

이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.

돌아가기

고유 필드 값 검색