|
1 | 1 | package docs.aggregation; |
2 | 2 |
|
| 3 | +// Hello codeowners |
| 4 | + |
3 | 5 | // begin imports |
4 | 6 | import com.mongodb.client.MongoClient; |
5 | 7 | import com.mongodb.client.MongoClients; |
|
20 | 22 | // begin class intro |
21 | 23 | // begin connection |
22 | 24 | public class AggTour { |
23 | | - public static void main(String[] args) { |
24 | | - // Replace the uri string with your MongoDB deployment's connection string |
25 | | - final String uri = "<connection string>"; |
26 | | - |
27 | | - MongoClient mongoClient = MongoClients.create(uri); |
28 | | - MongoDatabase database = mongoClient.getDatabase("aggregation"); |
29 | | - MongoCollection<Document> collection = database.getCollection("restaurants"); |
30 | | - // end connection |
| 25 | + public static void main(String[] args) { |
| 26 | + // Replace the uri string with your MongoDB deployment's connection string |
| 27 | + final String uri = "<connection string>"; |
| 28 | + |
| 29 | + MongoClient mongoClient = MongoClients.create(uri); |
| 30 | + MongoDatabase database = mongoClient.getDatabase("aggregation"); |
| 31 | + MongoCollection<Document> collection = database.getCollection("restaurants"); |
| 32 | + // end connection |
31 | 33 |
|
32 | | - collection.drop(); |
33 | | - |
34 | | - // Inserts sample documents describing restaurants |
35 | | - // begin insert |
36 | | - collection.insertMany(Arrays.asList( |
37 | | - new Document( "name", "Sun Bakery Trattoria"). append( "contact", new Document(). append( "phone", "386-555-0189"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 74.0056649, 40.7452371))). append( "stars", 4). append( "categories", Arrays. asList( "Pizza", "Pasta", "Italian", "Coffee", "Sandwiches")), |
38 | | - new Document( "name", "Blue Bagels Grill"). append( "contact", new Document(). append( "phone", "786-555-0102"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.92506, 40.8275556))). append( "stars", 3). append( "categories", Arrays. asList( "Bagels", "Cookies", "Sandwiches")), |
39 | | - new Document( "name", "XYZ Bagels Restaurant"). append( "contact", new Document(). append( "phone", "435-555-0190"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 74.0707363, 40.59321569999999))). append( "stars", 4). append( "categories", Arrays. asList( "Bagels", "Sandwiches", "Coffee")), |
40 | | - new Document( "name", "Hot Bakery Cafe"). append( "contact", new Document(). append( "phone", "264-555-0171"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.96485799999999, 40.761899))). append( "stars", 4). append( "categories", Arrays. asList( "Bakery", "Cafe", "Coffee", "Dessert")), |
41 | | - new Document( "name", "Green Feast Pizzeria"). append( "contact", new Document(). append( "phone", "840-555-0102"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 74.1220973, 40.6129407))). append( "stars", 2). append( "categories", Arrays. asList( "Pizza", "Italian")), |
42 | | - new Document( "name", "ZZZ Pasta Buffet"). append( "contact", new Document(). append( "phone", "769-555-0152"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.9446421, 40.7253944))). append( "stars", 0). append( "categories", Arrays. asList( "Pasta", "Italian", "Buffet", "Cafeteria")), |
43 | | - new Document( "name", "XYZ Coffee Bar"). append( "contact", new Document(). append( "phone", "644-555-0193"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 74.0166091, 40.6284767))). append( "stars", 5). append( "categories", Arrays. asList( "Coffee", "Cafe", "Bakery", "Chocolates")), |
44 | | - new Document( "name", "456 Steak Restaurant"). append( "contact", new Document(). append( "phone", "990-555-0165"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.9365108, 40.8497077))). append( "stars", 0). append( "categories", Arrays. asList( "Steak", "Seafood")), |
45 | | - new Document( "name", "456 Cookies Shop"). append( "contact", new Document(). append( "phone", "604-555-0149"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.8850023, 40.7494272))). append( "stars", 4). append( "categories", Arrays. asList( "Bakery", "Cookies", "Cake", "Coffee")), |
46 | | - new Document( "name", "XYZ Steak Buffet"). append( "contact", new Document(). append( "phone", "229-555-0197"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.9799932, 40.7660886))). append( "stars", 3). append( "categories", Arrays. asList( "Steak", "Salad", "Chinese")) |
47 | | - )); |
48 | | - // end insert |
| 34 | + collection.drop(); |
| 35 | + |
| 36 | + // Inserts sample documents describing restaurants |
| 37 | + // begin insert |
| 38 | + collection.insertMany(Arrays.asList( |
| 39 | + new Document( "name", "Sun Bakery Trattoria"). append( "contact", new Document(). append( "phone", "386-555-0189"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 74.0056649, 40.7452371))). append( "stars", 4). append( "categories", Arrays. asList( "Pizza", "Pasta", "Italian", "Coffee", "Sandwiches")), |
| 40 | + new Document( "name", "Blue Bagels Grill"). append( "contact", new Document(). append( "phone", "786-555-0102"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.92506, 40.8275556))). append( "stars", 3). append( "categories", Arrays. asList( "Bagels", "Cookies", "Sandwiches")), |
| 41 | + new Document( "name", "XYZ Bagels Restaurant"). append( "contact", new Document(). append( "phone", "435-555-0190"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 74.0707363, 40.59321569999999))). append( "stars", 4). append( "categories", Arrays. asList( "Bagels", "Sandwiches", "Coffee")), |
| 42 | + new Document( "name", "Hot Bakery Cafe"). append( "contact", new Document(). append( "phone", "264-555-0171"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.96485799999999, 40.761899))). append( "stars", 4). append( "categories", Arrays. asList( "Bakery", "Cafe", "Coffee", "Dessert")), |
| 43 | + new Document( "name", "Green Feast Pizzeria"). append( "contact", new Document(). append( "phone", "840-555-0102"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 74.1220973, 40.6129407))). append( "stars", 2). append( "categories", Arrays. asList( "Pizza", "Italian")), |
| 44 | + new Document( "name", "ZZZ Pasta Buffet"). append( "contact", new Document(). append( "phone", "769-555-0152"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.9446421, 40.7253944))). append( "stars", 0). append( "categories", Arrays. asList( "Pasta", "Italian", "Buffet", "Cafeteria")), |
| 45 | + new Document( "name", "XYZ Coffee Bar"). append( "contact", new Document(). append( "phone", "644-555-0193"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 74.0166091, 40.6284767))). append( "stars", 5). append( "categories", Arrays. asList( "Coffee", "Cafe", "Bakery", "Chocolates")), |
| 46 | + new Document( "name", "456 Steak Restaurant"). append( "contact", new Document(). append( "phone", "990-555-0165"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.9365108, 40.8497077))). append( "stars", 0). append( "categories", Arrays. asList( "Steak", "Seafood")), |
| 47 | + new Document( "name", "456 Cookies Shop"). append( "contact", new Document(). append( "phone", "604-555-0149"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.8850023, 40.7494272))). append( "stars", 4). append( "categories", Arrays. asList( "Bakery", "Cookies", "Cake", "Coffee")), |
| 48 | + new Document( "name", "XYZ Steak Buffet"). append( "contact", new Document(). append( "phone", "229-555-0197"). append( "email", "[email protected]"). append( "location", Arrays. asList(- 73.9799932, 40.7660886))). append( "stars", 3). append( "categories", Arrays. asList( "Steak", "Salad", "Chinese")) |
| 49 | + )); |
| 50 | + // end insert |
49 | 51 |
|
50 | | - // Creates an aggregation pipeline that matches documents, groups them by the "stars" field, and tallies them by distinct values |
51 | | - // begin aggregation basic |
52 | | - collection.aggregate( |
53 | | - Arrays.asList( |
54 | | - Aggregates.match(Filters.eq("categories", "Bakery")), |
55 | | - Aggregates.group("$stars", Accumulators.sum("count", 1)) |
56 | | - ) |
57 | | - // Prints the result of the aggregation operation as JSON |
58 | | - ).forEach(doc -> System.out.println(doc.toJson())); |
59 | | - // end aggregation basic |
| 52 | + // Creates an aggregation pipeline that matches documents, groups them by the "stars" field, and tallies them by distinct values |
| 53 | + // begin aggregation basic |
| 54 | + collection.aggregate( |
| 55 | + Arrays.asList( |
| 56 | + Aggregates.match(Filters.eq("categories", "Bakery")), |
| 57 | + Aggregates.group("$stars", Accumulators.sum("count", 1)) |
| 58 | + ) |
| 59 | + // Prints the result of the aggregation operation as JSON |
| 60 | + ).forEach(doc -> System.out.println(doc.toJson())); |
| 61 | + // end aggregation basic |
60 | 62 |
|
61 | | - // begin aggregation explain |
62 | | - Document explanation = collection.aggregate( |
63 | | - Arrays.asList( |
64 | | - Aggregates.match(Filters.eq("categories", "Bakery")), |
65 | | - Aggregates.group("$stars", Accumulators.sum("count", 1)) |
66 | | - ) |
67 | | - ).explain(ExplainVerbosity.EXECUTION_STATS); |
| 63 | + // begin aggregation explain |
| 64 | + Document explanation = collection.aggregate( |
| 65 | + Arrays.asList( |
| 66 | + Aggregates.match(Filters.eq("categories", "Bakery")), |
| 67 | + Aggregates.group("$stars", Accumulators.sum("count", 1)) |
| 68 | + ) |
| 69 | + ).explain(ExplainVerbosity.EXECUTION_STATS); |
68 | 70 |
|
69 | | - String winningPlans = explanation |
70 | | - .getEmbedded( |
71 | | - Arrays.asList("queryPlanner", "winningPlan", "queryPlan"), |
72 | | - Document.class |
73 | | - ) |
74 | | - .toJson(JsonWriterSettings.builder().indent(true).build()); |
| 71 | + String winningPlans = explanation |
| 72 | + .getEmbedded( |
| 73 | + Arrays.asList("queryPlanner", "winningPlan", "queryPlan"), |
| 74 | + Document.class |
| 75 | + ) |
| 76 | + .toJson(JsonWriterSettings.builder().indent(true).build()); |
75 | 77 |
|
76 | | - System.out.println(winningPlans); |
77 | | - // end aggregation explain |
| 78 | + System.out.println(winningPlans); |
| 79 | + // end aggregation explain |
78 | 80 |
|
79 | | - // Prints the restaurant name and the first value in the "categories" array as a field named "firstCategory" |
80 | | - // begin aggregation expression |
81 | | - collection.aggregate( |
82 | | - Arrays.asList( |
83 | | - Aggregates.project( |
84 | | - Projections.fields( |
85 | | - Projections.excludeId(), |
86 | | - Projections.include("name"), |
87 | | - Projections.computed( |
88 | | - "firstCategory", |
89 | | - new Document( |
90 | | - "$arrayElemAt", |
91 | | - Arrays.asList("$categories", 0) |
92 | | - ) |
93 | | - ) |
94 | | - ) |
95 | | - ) |
96 | | - ) |
97 | | - ).forEach(doc -> System.out.println(doc.toJson())); |
98 | | - // end aggregation expression |
99 | | - } |
| 81 | + // Prints the restaurant name and the first value in the "categories" array as a field named "firstCategory" |
| 82 | + // begin aggregation expression |
| 83 | + collection.aggregate( |
| 84 | + Arrays.asList( |
| 85 | + Aggregates.project( |
| 86 | + Projections.fields( |
| 87 | + Projections.excludeId(), |
| 88 | + Projections.include("name"), |
| 89 | + Projections.computed( |
| 90 | + "firstCategory", |
| 91 | + new Document( |
| 92 | + "$arrayElemAt", |
| 93 | + Arrays.asList("$categories", 0) |
| 94 | + ) |
| 95 | + ) |
| 96 | + ) |
| 97 | + ) |
| 98 | + ) |
| 99 | + ).forEach(doc -> System.out.println(doc.toJson())); |
| 100 | + // end aggregation expression |
| 101 | + } |
100 | 102 | } |
0 commit comments