Hướng dẫn how aggregation is used in mongodb in java? - cách tổng hợp được sử dụng trong mongodb trong java?
DOCS HOME → SYNC JAVA → Java Sync Show
Trên trang này
Trong hướng dẫn này, bạn có thể tìm hiểu cách sử dụng các hoạt động tổng hợp trong trình điều khiển Java MongoDB.aggregation operations in the MongoDB Java driver. Tổng hợp hoạt động xử lý dữ liệu trong các bộ sưu tập MongoDB của bạn và trả về kết quả được tính toán. Đường ống tổng hợp của MongoDB, một phần của API truy vấn, được mô hình hóa trên khái niệm các đường ống xử lý dữ liệu. Các tài liệu nhập một đường ống nhiều giai đoạn chuyển đổi các tài liệu thành một kết quả tổng hợp. Một cách khác để nghĩ về tổng hợp giống như một nhà máy xe hơi. Trong nhà máy sản xuất xe hơi là một dây chuyền lắp ráp, cùng với các trạm lắp ráp với các công cụ chuyên dụng để thực hiện một công việc cụ thể, như máy khoan và thợ hàn. Các bộ phận thô vào nhà máy, sau đó được chuyển đổi và lắp ráp thành một sản phẩm hoàn chỉnh. Đường ống tổng hợp là dây chuyền lắp ráp, các giai đoạn tổng hợp là các trạm lắp ráp và các biểu thức vận hành là các công cụ chuyên dụng.aggregation pipeline is the assembly line, aggregation stages are the assembly stations, and operator expressions are the specialized tools. Sử dụng các hoạt động 1, bạn có thể:
Sử dụng các hoạt động 2, bạn có thể:
Các hoạt động tổng hợp có một số hạn chế bạn phải ghi nhớ:limitations you must keep in mind:
Tạo một tệp Java mới có tên 5 và bao gồm các câu lệnh nhập sau:
Mẹo
Để thực hiện một tập hợp, hãy truyền một danh sách các giai đoạn tổng hợp cho phương thức 6.Trình điều khiển Java cung cấp lớp trợ giúp tổng hợp có chứa các nhà xây dựng cho các giai đoạn tổng hợp.Aggregates helper class that contains builders for aggregation stages. Trong ví dụ sau, đường ống tổng hợp:
MẹoĐể thực hiện một tập hợp, hãy truyền một danh sách các giai đoạn tổng hợp cho phương thức public class AggTour { public static void main(String[] args) { // Replace the uri string with your MongoDB deployment's connection string String uri = " |
collection.aggregate( |
Arrays.asList( |
Aggregates.match(Filters.eq("categories", "Bakery")), |
Aggregates.group("$stars", Accumulators.sum("count", 1)) |
) |
).forEach(doc -> System.out.println(doc.toJson())); |
Trình điều khiển Java cung cấp lớp trợ giúp tổng hợp có chứa các nhà xây dựng cho các giai đoạn tổng hợp.
{"_id": 4, "count": 2} {"_id": 5, "count": 1}
Trong ví dụ sau, đường ống tổng hợp:
MongoCollection.aggregate()
Aggregates.match
Sử dụng giai đoạn Match $ để lọc cho các tài liệu có trường mảng
public class AggTour { public static void main(String[] args) { // Replace the uri string with your MongoDB deployment's connection string String uri = "" ; MongoClient mongoClient = MongoClients.create(uri); MongoDatabase database = mongoClient.getDatabase("aggregation"); MongoCollection collection = database.getCollection("restaurants"); // aggregation here } }
7 chứa phần tử public class AggTour { public static void main(String[] args) { // Replace the uri string with your MongoDB deployment's connection string String uri = "" ; MongoClient mongoClient = MongoClients.create(uri); MongoDatabase database = mongoClient.getDatabase("aggregation"); MongoCollection collection = database.getCollection("restaurants"); // aggregation here } }
8. Ví dụ sử dụng public class AggTour { public static void main(String[] args) { // Replace the uri string with your MongoDB deployment's connection string String uri = "" ; MongoClient mongoClient = MongoClients.create(uri); MongoDatabase database = mongoClient.getDatabase("aggregation"); MongoCollection collection = database.getCollection("restaurants"); // aggregation here } }
9 để xây dựng giai đoạn collection.insertMany(Arrays.asList( new Document("name", "Sun Bakery Trattoria").append("contact", new Document().append("phone", "386-555-0189").append("email", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").append("location", Arrays.asList(-73.9799932, 40.7660886))).append("stars", 3).append("categories", Arrays.asList("Steak", "Salad", "Chinese")) ));
0.execution plans and performance statistics. An execution plan is a potential
way MongoDB can complete an operation. The collection.insertMany(Arrays.asList( new Document("name", "Sun Bakery Trattoria").append("contact", new Document().append("phone", "386-555-0189").append("email", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").append("location", Arrays.asList(-73.9799932, 40.7660886))).append("stars", 3).append("categories", Arrays.asList("Steak", "Salad", "Chinese")) ));
3 method provides both the winning plan (the plan MongoDB executed) and rejected plans.Sử dụng một nhóm nhóm $ để nhóm các tài liệu phù hợp theo trường
collection.insertMany(Arrays.asList( new Document("name", "Sun Bakery Trattoria").append("contact", new Document().append("phone", "386-555-0189").append("email", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").append("location", Arrays.asList(-73.9799932, 40.7660886))).append("stars", 3).append("categories", Arrays.asList("Steak", "Salad", "Chinese")) ));
1, tích lũy số lượng tài liệu cho từng giá trị riêng biệt là collection.insertMany(Arrays.asList( new Document("name", "Sun Bakery Trattoria").append("contact", new Document().append("phone", "386-555-0189").append("email", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").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", "").append("location", Arrays.asList(-73.9799932, 40.7660886))).append("stars", 3).append("categories", Arrays.asList("Steak", "Salad", "Chinese")) ));
1.Xem thêm:
Tập hợp trước sẽ tạo ra các kết quả sau: | Để biết thêm thông tin về các phương thức và lớp được đề cập trong phần này, hãy xem tài liệu API sau: | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ALL_PLANS_EXECUTIONS | Để xem thông tin về cách MongoDB thực hiện thao tác của bạn, hãy sử dụng phương pháp | ||||||||||||||||||||||||||||||||||||||||||||||||
EXECUTION_STATS | Bạn có thể chỉ định mức độ chi tiết của lời giải thích của mình bằng cách chuyển mức độ xác thực cho phương pháp | ||||||||||||||||||||||||||||||||||||||||||||||||
QUERY_PLANNER | Bảng sau đây cho thấy tất cả các mức độ dài cho các giải thích và các trường hợp sử dụng dự định của chúng: |
Mức độ verbosity
Document explanation = collection.aggregate( Arrays.asList( Aggregates.match(Filters.eq("categories", "bakery")), Aggregates.group("$stars", Accumulators.sum("count", 1)) ) ).explain(ExplainVerbosity.EXECUTION_STATS); List stages = explanation.get("stages", List.class); List keys = Arrays.asList("queryPlanner", "winningPlan"); for (Document stage : stages) { Document cursorStage = stage.get("$cursor", Document.class); if (cursorStage != null) { System.out.println(cursorStage.getEmbedded(keys, Document.class).toJson()); } }
Sử dụng trường hợp
{ "stage": "PROJECTION_SIMPLE", "transformBy": {"stars": 1, "_id": 0}, "inputStage": { "stage": "COLLSCAN", "filter": { "categories": {"$eq":"bakery"}}, "direction": "forward"}}
Bạn muốn biết kế hoạch nào MongoDB sẽ chọn để chạy truy vấn của bạn.
Bạn muốn biết nếu truy vấn của bạn đang hoạt động tốt. Server Manual Entry
Bạn có vấn đề với truy vấn của bạn và bạn muốn càng nhiều thông tin càng tốt để chẩn đoán vấn đề. Server Manual Entry
Trong ví dụ sau, chúng tôi in biểu diễn JSON của các kế hoạch chiến thắng cho các giai đoạn tổng hợp tạo ra các kế hoạch thực hiện: API Documentation
Đoạn mã trước sẽ tạo ra đầu ra sau: API Documentation
Để biết thêm thông tin về các chủ đề được đề cập trong phần này, hãy xem các tài nguyên sau: API Documentation
Giải thích mục nhập hướng dẫn đầu ra của máy chủ
Mẹo
Để thực hiện một tập hợp, hãy truyền một danh sách các giai đoạn tổng hợp cho phương thức
public class AggTour { public static void main(String[] args) { // Replace the uri string with your MongoDB deployment's connection string String uri = "" ; MongoClient mongoClient = MongoClients.create(uri); MongoDatabase database = mongoClient.getDatabase("aggregation"); MongoCollection collection = database.getCollection("restaurants"); // aggregation here } }
6.$arrayElemAt expression.Trình điều khiển Java cung cấp lớp trợ giúp tổng hợp có chứa các nhà xây dựng cho các giai đoạn tổng hợp.field path, using the "categories" field from the input document.
new Document("$arrayElemAt", Arrays.asList("$categories", 0))
Document.parse("{ $arrayElemAt: ['$categories', 0] }")
Trong ví dụ sau, đường ống tổng hợp:
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()));
Tập hợp trước sẽ tạo ra các kết quả sau:
public class AggTour { public static void main(String[] args) { // Replace the uri string with your MongoDB deployment's connection string String uri = "" ; MongoClient mongoClient = MongoClients.create(uri); MongoDatabase database = mongoClient.getDatabase("aggregation"); MongoCollection collection = database.getCollection("restaurants"); // aggregation here } }
0Để biết thêm thông tin về các phương thức và lớp được đề cập trong phần này, hãy xem tài liệu API sau:
Người tích lũy
$group
$project
Dự đoán