문서 메뉴

문서 홈애플리케이션 개발MongoDB 드라이버Java 동기화

집계

이 페이지의 내용

  • 개요
  • 집계 및 찾기 작업 비교
  • 유용한 참고 자료
  • 실행 가능한 예제
  • 기본 설정
  • 기본 애그리게이션 예시
  • 애그리게이션 설명 예시
  • 애그리게이션 표현식 예시

이 가이드에서는 MongoDB Java 드라이버에서 애그리게이션 작업 을 사용하는 방법을 배울 수 있습니다.

애그리게이션 작업은 MongoDB collection의 데이터를 처리하고 계산된 결과를 반환합니다. 쿼리 API의 일부인 MongoDB의 집계 파이프라인은 데이터 처리 파이프라인 개념을 모델로 합니다. 문서는 문서를 애그리게이션된 결과로 변환하는 다단계 파이프라인에 들어갑니다.

애그리게이션을 생각하는 또 다른 방식은 자동차 공장과 같습니다. 자동차 공장 내에는 조립 라인이 있으며, 여기에는 드릴과 용접기 등 특정 작업을 수행할 수 있는 특수 공구를 갖춘 조립 스테이션이 있습니다. 원부품이 공장에 들어오고, 공장은 변형되어 완제품으로 조립됩니다.

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

find 작업을 사용하면 다음을 수행할 수 있습니다.

  • 반환할 문서 선택

  • 반환할 필드 선택

  • 결과 정렬

aggregation 작업을 사용하면 다음을 수행할 수 있습니다.

  • 모든 find 작업 수행

  • 필드 이름 바꾸기

  • 필드 계산

  • 데이터 요약

  • 그룹 값

애그리게이션 작업에는 몇 가지 제한 사항 을 염두에 두어야 합니다.

  • 반환된 문서는 BSON 문서 크기 제한 인 16 메가바이트를 위반하지 않아야 합니다.

  • 파이프라인 단계의 메모리 100 제한은 기본적으로 메가바이트입니다. 필요한 경우 allowDiskUse 를 사용하여 이 제한을 초과할 수 있습니다. 메서드.

    중요

    $graphLookup 예외

    $graphLookup 단계에는 100 메가바이트의 엄격한 메모리 제한이 있으며 allowDiskUse 을(를) 무시합니다.

AggTour.java라는 새 Java 파일을 만들고 다음 가져오기 성명서를 포함합니다.

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.ExplainVerbosity;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import org.bson.Document;
import java.util.Arrays;
import java.util.List;
public class AggTour {
public static void main(String[] args) {
// Replace the uri string with your MongoDB deployment's connection string
String uri = "<connection string uri>";
MongoClient mongoClient = MongoClients.create(uri);
MongoDatabase database = mongoClient.getDatabase("aggregation");
MongoCollection<Document> collection = database.getCollection("restaurants");
// aggregation here
}
}

다음도 참조하세요.

MongoDB 연결에 대한 자세한 내용은 연결 가이드를 참조하세요.

collection.insertMany(Arrays.asList(
new Document("name", "Sun Bakery Trattoria").append("contact", new Document().append("phone", "386-555-0189").append("email", "SunBakeryTrattoria@example.org").append("location", Arrays.asList(-74.0056649, 40.7452371))).append("stars", 4).append("categories", Arrays.asList("Pizza", "Pasta", "Italian", "Coffee", "Sandwiches")),
new Document("name", "Blue Bagels Grill").append("contact", new Document().append("phone", "786-555-0102").append("email", "BlueBagelsGrill@example.com").append("location", Arrays.asList(-73.92506, 40.8275556))).append("stars", 3).append("categories", Arrays.asList("Bagels", "Cookies", "Sandwiches")),
new Document("name", "XYZ Bagels Restaurant").append("contact", new Document().append("phone", "435-555-0190").append("email", "XYZBagelsRestaurant@example.net").append("location", Arrays.asList(-74.0707363, 40.59321569999999))).append("stars", 4).append("categories", Arrays.asList("Bagels", "Sandwiches", "Coffee")),
new Document("name", "Hot Bakery Cafe").append("contact", new Document().append("phone", "264-555-0171").append("email", "HotBakeryCafe@example.net").append("location", Arrays.asList(-73.96485799999999, 40.761899))).append("stars", 4).append("categories", Arrays.asList("Bakery", "Cafe", "Coffee", "Dessert")),
new Document("name", "Green Feast Pizzeria").append("contact", new Document().append("phone", "840-555-0102").append("email", "GreenFeastPizzeria@example.com").append("location", Arrays.asList(-74.1220973, 40.6129407))).append("stars", 2).append("categories", Arrays.asList("Pizza", "Italian")),
new Document("name", "ZZZ Pasta Buffet").append("contact", new Document().append("phone", "769-555-0152").append("email", "ZZZPastaBuffet@example.com").append("location", Arrays.asList(-73.9446421, 40.7253944))).append("stars", 0).append("categories", Arrays.asList("Pasta", "Italian", "Buffet", "Cafeteria")),
new Document("name", "XYZ Coffee Bar").append("contact", new Document().append("phone", "644-555-0193").append("email", "XYZCoffeeBar@example.net").append("location", Arrays.asList(-74.0166091, 40.6284767))).append("stars", 5).append("categories", Arrays.asList("Coffee", "Cafe", "Bakery", "Chocolates")),
new Document("name", "456 Steak Restaurant").append("contact", new Document().append("phone", "990-555-0165").append("email", "456SteakRestaurant@example.com").append("location", Arrays.asList(-73.9365108, 40.8497077))).append("stars", 0).append("categories", Arrays.asList("Steak", "Seafood")),
new Document("name", "456 Cookies Shop").append("contact", new Document().append("phone", "604-555-0149").append("email", "456CookiesShop@example.org").append("location", Arrays.asList(-73.8850023, 40.7494272))).append("stars", 4).append("categories", Arrays.asList("Bakery", "Cookies", "Cake", "Coffee")),
new Document("name", "XYZ Steak Buffet").append("contact", new Document().append("phone", "229-555-0197").append("email", "XYZSteakBuffet@example.org").append("location", Arrays.asList(-73.9799932, 40.7660886))).append("stars", 3).append("categories", Arrays.asList("Steak", "Salad", "Chinese"))
));

애그리게이션을 수행하려면 애그리게이션 단계 목록을 MongoCollection.aggregate() 메서드에 전달합니다.

Java 드라이버는 다음을 제공합니다 . 애그리게이션 단계에 대한 빌더가 포함된 헬퍼 클래스입니다.

다음 예제에서 애그리게이션 파이프라인은 다음과 같습니다.

  • $match 단계를 사용하여 categories 배열 필드에 Bakery 요소가 포함된 문서를 필터링합니다. 이 예제에서는 Aggregates.match 를 사용하여 $match 단계를 빌드합니다.

  • $group 단계를 사용하여 일치하는 문서를 stars 필드별로 그룹화하여 stars 의 각 고유 값에 대한 문서 수를 누적합니다.

다음도 참조하세요.

애그리게이션 빌더를 사용하여 이 예제에 사용된 표현식을 작성할 수 있습니다.

collection.aggregate(
Arrays.asList(
Aggregates.match(Filters.eq("categories", "Bakery")),
Aggregates.group("$stars", Accumulators.sum("count", 1))
)
// Prints the result of the aggregation operation as JSON
).forEach(doc -> System.out.println(doc.toJson()));

앞의 애그리게이션은 다음과 같은 결과를 생성해야 합니다.

{"_id": 4, "count": 2}
{"_id": 5, "count": 1}

이 섹션에 언급된 메서드 및 클래스에 대한 자세한 내용은 다음 API 문서를 참조하세요.

MongoDB가 작업을 실행하는 방법에 대한 정보를 보려면 AggregateIterable 클래스의 explain() 메서드를 사용합니다. explain() 메서드는 실행 계획 및 성능 통계를 반환합니다. 실행 계획은 MongoDB가 작업을 완료할 수 있는 잠재적인 방법입니다. explain() 메서드는 성공적인 계획(MongoDB가 실행한 계획)과 거부된 계획을 모두 제공합니다.

explain() 메소드에 상세 수준을 전달하여 설명의 세부 정보 수준을 지정할 수 있습니다.

다음 표에는 설명 및 해당 사용 사례에 대한 모든 상세 수준이 나와 있습니다.

상세도 수준
사용 사례
ALL_PLANS_EXECUTIONS
쿼리를 실행하기 위해 MongoDB가 어떤 계획을 선택할지 알고 싶습니다.
EXECUTION_STATS
쿼리가 잘 수행되고 있는지 알고 싶습니다.
QUERY_PLANNER
쿼리에 문제가 있으며 문제를 진단하기 위해 가능한 한 많은 정보를 원합니다.

다음 예제에서는 실행 계획을 생성하는 애그리게이션 단계에 대한 성공적인 계획의 JSON 표현을 인쇄합니다.

Document explanation = collection.aggregate(
Arrays.asList(
Aggregates.match(Filters.eq("categories", "Bakery")),
Aggregates.group("$stars", Accumulators.sum("count", 1))
)
).explain(ExplainVerbosity.EXECUTION_STATS);
List<Document> stages = explanation.get("stages", List.class);
List<String> keys = Arrays.asList("queryPlanner", "winningPlan");
// Prints the JSON representation of the winning execution plans
for (Document stage : stages) {
Document cursorStage = stage.get("$cursor", Document.class);
if (cursorStage != null) {
System.out.println(cursorStage.getEmbedded(keys, Document.class).toJson());
}
}

앞의 코드 스니펫은 다음과 같은 출력을 생성해야 합니다.

{ "stage": "PROJECTION_SIMPLE",
"transformBy": {"stars": 1, "_id": 0},
"inputStage": {
"stage": "COLLSCAN",
"filter": {
"categories": {"$eq":"bakery"}},
"direction": "forward"}}

이 섹션에 언급된 주제에 대한 자세한 내용은 다음 리소스를 참조하세요.

Java 드라이버는 $group에 사용할 축적자 표현식에 대한 빌더를 제공합니다. 다른 모든 표현식은 JSON 형식 또는 호환되는 문서 형식으로 선언해야 합니다.

다음 예제 중 하나의 구문은 $arrayElemAt 표현식을 정의합니다.

'카테고리' 앞의 $ 는 입력 문서의 '카테고리' 필드를 사용하여 이것이 필드 경로 임을 MongoDB에 알립니다.

new Document("$arrayElemAt", Arrays.asList("$categories", 0))
Document.parse("{ $arrayElemAt: ['$categories', 0] }")

다음 예제에서 aggregation pipeline은 $project 단계와 다양한 Projections를 사용하여 name 필드와 categories 필드의 첫 번째 요소가 값인 계산된 필드 firstCategory를 반환합니다.

collection.aggregate(
Arrays.asList(
Aggregates.project(
Projections.fields(
Projections.excludeId(),
Projections.include("name"),
Projections.computed(
"firstCategory",
new Document("$arrayElemAt", Arrays.asList("$categories", 0))
)
)
)
)
).forEach(doc -> System.out.println(doc.toJson()));

앞의 애그리게이션은 다음과 같은 결과를 생성해야 합니다.

{"name": "456 Cookies Shop", "firstCategory": "Bakery"}
{"name": "Sun Bakery Trattoria", "firstCategory": "Pizza"}
{"name": "456 Steak Restaurant", "firstCategory": "Steak"}
{"name": "Blue Bagels Grill", "firstCategory": "Bagels"}
{"name": "XYZ Steak Buffet", "firstCategory": "Steak"}
{"name": "Hot Bakery Cafe", "firstCategory": "Bakery"}
{"name": "Green Feast Pizzeria", "firstCategory": "Pizza"}
{"name": "ZZZ Pasta Buffet", "firstCategory": "Pasta"}
{"name": "XYZ Coffee Bar", "firstCategory": "Coffee"}
{"name": "XYZ Bagels Restaurant", "firstCategory": "Bagels"}

이 섹션에 언급된 메서드 및 클래스에 대한 자세한 내용은 다음 API 문서를 참조하세요.

← 빌더 업데이트