diff --git a/source/includes/fundamentals/code-snippets/AggTour.java b/source/includes/fundamentals/code-snippets/AggTour.java index e55321d22..a0b8f4a15 100644 --- a/source/includes/fundamentals/code-snippets/AggTour.java +++ b/source/includes/fundamentals/code-snippets/AggTour.java @@ -1,5 +1,7 @@ package docs.aggregation; +// Hello codeowners + // begin imports import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; @@ -20,81 +22,81 @@ // begin class intro // begin connection public class AggTour { - public static void main(String[] args) { - // Replace the uri string with your MongoDB deployment's connection string - final String uri = ""; - - MongoClient mongoClient = MongoClients.create(uri); - MongoDatabase database = mongoClient.getDatabase("aggregation"); - MongoCollection collection = database.getCollection("restaurants"); - // end connection + public static void main(String[] args) { + // Replace the uri string with your MongoDB deployment's connection string + final String uri = ""; + + MongoClient mongoClient = MongoClients.create(uri); + MongoDatabase database = mongoClient.getDatabase("aggregation"); + MongoCollection collection = database.getCollection("restaurants"); + // end connection - collection.drop(); - - // Inserts sample documents describing restaurants - // begin insert - 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")) - )); - // end insert + collection.drop(); + + // Inserts sample documents describing restaurants + // begin insert + 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")) + )); + // end insert - // Creates an aggregation pipeline that matches documents, groups them by the "stars" field, and tallies them by distinct values - // begin aggregation basic - 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())); - // end aggregation basic + // Creates an aggregation pipeline that matches documents, groups them by the "stars" field, and tallies them by distinct values + // begin aggregation basic + 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())); + // end aggregation basic - // begin aggregation explain - Document explanation = collection.aggregate( - Arrays.asList( - Aggregates.match(Filters.eq("categories", "Bakery")), - Aggregates.group("$stars", Accumulators.sum("count", 1)) - ) - ).explain(ExplainVerbosity.EXECUTION_STATS); + // begin aggregation explain + Document explanation = collection.aggregate( + Arrays.asList( + Aggregates.match(Filters.eq("categories", "Bakery")), + Aggregates.group("$stars", Accumulators.sum("count", 1)) + ) + ).explain(ExplainVerbosity.EXECUTION_STATS); - String winningPlans = explanation - .getEmbedded( - Arrays.asList("queryPlanner", "winningPlan", "queryPlan"), - Document.class - ) - .toJson(JsonWriterSettings.builder().indent(true).build()); + String winningPlans = explanation + .getEmbedded( + Arrays.asList("queryPlanner", "winningPlan", "queryPlan"), + Document.class + ) + .toJson(JsonWriterSettings.builder().indent(true).build()); - System.out.println(winningPlans); - // end aggregation explain + System.out.println(winningPlans); + // end aggregation explain - // Prints the restaurant name and the first value in the "categories" array as a field named "firstCategory" - // begin aggregation expression - 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())); - // end aggregation expression - } + // Prints the restaurant name and the first value in the "categories" array as a field named "firstCategory" + // begin aggregation expression + 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())); + // end aggregation expression + } }