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

애그리게이션을 통한 데이터 변환

이 가이드 에서는 C++ 운전자 를 사용하여 집계 작업 을 수행하는 방법을 학습 수 있습니다.

애그리게이션 작업은 MongoDB 컬렉션의 데이터를 처리하고 계산된 결과를 반환합니다. Query API의 일부인 MongoDB 애그리게이션 프레임워크는 데이터 처리 파이프라인 개념을 모델로 합니다. 문서는 하나 이상의 단계를 포함하는 파이프라인에 들어가고, 이 파이프라인은 문서를 애그리게이션된 결과로 변환합니다.

팁

애그리게이션 튜토리얼 완료

You can find tutorials that provide detailed explanations of common aggregation tasks in the Complete Aggregation Pipeline Tutorials section of the Server manual. Select a tutorial, and then pick C++11 from the Select your language drop-down menu in the upper-right corner of the page.

집계 작업은 자동차 공장과 유사합니다. 자동차 공장에는 조립 라인이 있으며, 여기에는 드릴과 용접기 등 특정 작업을 수행할 수 있는 특수 공구를 갖춘 조립 스테이션이 있습니다. 원부품이 공장에 들어오면 조립 라인에서 이를 변형하고 조립하여 완제품으로 만듭니다.

집계 파이프라인은 조립 라인이고, 집계 단계는 조립 스테이션이며, 작업 연산자는 특수 도구입니다.

찾기 조치를 사용하여 다음 조치을 수행할 수 있습니다:

  • 반환할 문서 선택

  • 반환할 필드 선택

  • 결과 정렬

집계 조치를 사용하여 다음 조치를 수행할 수 있습니다:

  • 찾기 작업 실행

  • 필드 이름 바꾸기

  • 필드 계산

  • 데이터 요약

  • 그룹 값

애그리게이션 작업을 사용할 때는 다음과 같은 제한 사항에 유의하세요.

  • Returned documents cannot violate the BSON document size limit of 16 megabytes.

  • 파이프라인 단계의 메모리 제한은 기본값 100 메가바이트입니다. mongocxx::options::aggregate 인스턴스 의 allow_disk_use 필드 를 true 로 설정하여 이 제한을 초과할 수 있습니다.

중요

$graphLookup 예외

The $graphLookup stage has a strict memory limit of 100 megabytes and ignores the allow_disk_use field.

참고

The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

집계 집계 단계가 포함된 mongocxx::pipeline 인스턴스 를 collection.aggregate() 메서드에 전달합니다.

다음 코드 예시 에서는 뉴욕의 각 자치구에 있는 베이커리 수의 개수를 생성합니다. 이를 위해 다음 단계가 포함된 집계 파이프라인 을 사용합니다.

  • $match stage to filter for documents in which the cuisine field contains the value "Bakery"

  • $group stage to group the matching documents by the borough field, accumulating a count of documents for each distinct value

mongocxx::pipeline stages;
stages.match(make_document(kvp("cuisine", "Bakery")))
.group(make_document(kvp("_id", "$borough"), kvp("count", make_document(kvp("$sum", 1)))));
auto cursor = collection.aggregate(stages);
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
{ "_id" : "Brooklyn", "count" : 173 }
{ "_id" : "Queens", "count" : 204 }
{ "_id" : "Bronx", "count" : 71 }
{ "_id" : "Staten Island", "count" : 20 }
{ "_id" : "Missing", "count" : 2 }
{ "_id" : "Manhattan", "count" : 221 }

MongoDB 가 작업을 실행하는 방법에 대한 정보를 보려면 MongoDB 쿼리 플래너에게 설명 을 지시하면 됩니다. MongoDB 는 작업을 설명할 때 실행 계획 과 성능 통계를 반환합니다. 실행 계획은 MongoDB 가 작업을 완료할 수 있는 잠재적인 방법입니다. MongoDB 에 작업 설명을 지시하면 MongoDB 가 실행한 계획과 거부된 실행 계획이 모두 반환됩니다.

집계 작업을 설명하려면 BSON 문서 에서 명령을 지정하고 이를 run_command() 메서드에 인수로 전달하여 explain 데이터베이스 명령 을 실행 합니다.

The following example instructs MongoDB to explain the aggregation operation from the preceding Aggregation Example:

mongocxx::pipeline stages;
stages.match(make_document(kvp("cuisine", "Bakery")))
.group(make_document(kvp("_id", "$borough"), kvp("count", make_document(kvp("$sum", 1)))));
auto command = make_document(
kvp("explain", make_document(
kvp("aggregate", "restaurants"),
kvp("pipeline", stages.view_array()),
kvp("cursor", make_document()))));
auto result = db.run_command(command.view());
std::cout << bsoncxx::to_json(result) << std::endl;
{ "explainVersion" : "2", "queryPlanner" : { "namespace" : "sample_restaurants.restaurants",
"indexFilterSet" : false, "parsedQuery" : { "cuisine" : { "$eq" : "Bakery" } }, "queryHash":
"...", "planCacheKey" : "...", "optimizedPipeline" : true, "maxIndexedOrSolutionsReached":
false, "maxIndexedAndSolutionsReached" : false, "maxScansToExplodeReached" : false,
"winningPlan" : { ... }
... }

For a full list of aggregation stages, see Aggregation Stages in the MongoDB Server manual.

집계 파이프라인을 어셈블하는 방법에 대해 알아보고 예제를 보려면 집계 파이프라인을 참조하세요.

MongoDB 작업에 대해 자세히 알아보려면 출력 설명 및 쿼리 계획을 참조하세요.

C++ 운전자 를 사용하여 집계 작업을 실행하는 방법에 대한 자세한 내용은 다음 API 설명서를 참조하세요.