diff --git a/docs/50-aggregation/2-match-project.mdx b/docs/50-aggregation/2-match-project.mdx index 8c32db6..3178f56 100644 --- a/docs/50-aggregation/2-match-project.mdx +++ b/docs/50-aggregation/2-match-project.mdx @@ -155,11 +155,13 @@ db.books.aggregate([
```java + import com.mongodb.client.model.Aggregates; + books.aggregate( List.of( Aggregates.match(gt( "available", 2))) - ).forEach(document - > System.out.println(document.toJson())); + ).forEach(document -> System.out.println(document.toJson())); ```
@@ -230,13 +232,16 @@ db.books.aggregate([
```java + import com.mongodb.client.model.Aggregates; + import com.mongodb.client.model.Projections; + books.aggregate( List.of( Aggregates.match(gt( "available", 2)), Aggregates.project(Projections.include("title", "year")), Aggregates.project(Projections.exclude("_id"))) - ).forEach(document - > System.out.println(document.toJson())); + ).forEach(document -> System.out.println(document.toJson())); ```
diff --git a/docs/50-aggregation/3-sort-limit.mdx b/docs/50-aggregation/3-sort-limit.mdx index ada9a51..79c98a2 100644 --- a/docs/50-aggregation/3-sort-limit.mdx +++ b/docs/50-aggregation/3-sort-limit.mdx @@ -250,6 +250,12 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
```java + import com.mongodb.client.model.Projections; + import com.mongodb.client.model.Field; + import com.mongodb.client.model.Sorts; + import static com.mongodb.client.model.Filters.exists; + import static com.mongodb.client.model.Filters.gt; + books.aggregate( List.of( Aggregates.match(gt("year", 2000)), @@ -275,21 +281,27 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
```java + import com.mongodb.client.model.Projections; + import com.mongodb.client.model.Field; + import com.mongodb.client.model.Sorts; + import static com.mongodb.client.model.Filters.exists; + import static com.mongodb.client.model.Filters.gt; + books.aggregate( List.of( - Aggregates.match(gt("year", 2000)), - Aggregates.match(exists("authors", true)), - Aggregates.addFields( - new Field<>( - "numAuthors", - new Document("$size", "$authors") - ) - ), - Aggregates.project( - Projections.fields( - Projections.include("title", "year", "authors", "numAuthors"))), - Aggregates.sort(Sorts.descending("numAuthors")), - Aggregates.limit(1) + Aggregates.match(gt("year", 2000)), + Aggregates.match(exists("authors", true)), + Aggregates.addFields( + new Field<>( + "numAuthors", + new Document("$size", "$authors") + ) + ), + Aggregates.project( + Projections.fields( + Projections.include("title", "year", "authors", "numAuthors"))), + Aggregates.sort(Sorts.descending("numAuthors")), + Aggregates.limit(1) ) ).forEach(c -> System.out.println(c.toJson())); ``` diff --git a/docs/50-aggregation/4-group.mdx b/docs/50-aggregation/4-group.mdx index 6e163af..f40ae45 100644 --- a/docs/50-aggregation/4-group.mdx +++ b/docs/50-aggregation/4-group.mdx @@ -194,6 +194,8 @@ GROUP BY year;
```java + MongoCollection reviews = library.getCollection("reviews"); + reviews.aggregate( List.of( Aggregates.group("$bookId", @@ -338,6 +340,10 @@ GROUP BY year;
```java + import static com.mongodb.client.model.Sorts.descending; + + MongoCollection reviews = library.getCollection("reviews"); + reviews.aggregate( List.of(Aggregates.group( "$name", @@ -351,6 +357,8 @@ GROUP BY year;
```java + MongoCollection reviews = library.getCollection("reviews"); + reviews.aggregate( List.of( Aggregates.sortByCount("$name")) diff --git a/docs/50-aggregation/5-lookup.mdx b/docs/50-aggregation/5-lookup.mdx index 432b913..0f44541 100644 --- a/docs/50-aggregation/5-lookup.mdx +++ b/docs/50-aggregation/5-lookup.mdx @@ -168,6 +168,10 @@ The $lookup operation creates an array within each book document. Using $unwind
```java + import com.mongodb.client.model.Aggregates; + + MongoCollection books = library.getCollection("books"); + books.aggregate( List.of( Aggregates.lookup( diff --git a/docs/50-aggregation/7-merge.mdx b/docs/50-aggregation/7-merge.mdx index ac29039..22a64d3 100644 --- a/docs/50-aggregation/7-merge.mdx +++ b/docs/50-aggregation/7-merge.mdx @@ -231,6 +231,8 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks); ```java + import com.mongodb.client.model.MergeOptions; + books.aggregate( List.of( Aggregates.unwind("$authors"), @@ -250,20 +252,22 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks); ```java - authors.aggregate( - List.of( - Aggregates.unwind("$books"), - Aggregates.group( - "$name", - Accumulators.sum("totalBooks", 1) - ), - Aggregates.merge( - "author_stats", - new MergeOptions() - .uniqueIdentifier("_id") - .whenMatched(MergeOptions.WhenMatched.MERGE) - .whenNotMatched(MergeOptions.WhenNotMatched.INSERT))) - ).toCollection(); + import com.mongodb.client.model.MergeOptions; + + authors.aggregate( + List.of( + Aggregates.unwind("$books"), + Aggregates.group( + "$name", + Accumulators.sum("totalBooks", 1) + ), + Aggregates.merge( + "author_stats", + new MergeOptions() + .uniqueIdentifier("_id") + .whenMatched(MergeOptions.WhenMatched.MERGE) + .whenNotMatched(MergeOptions.WhenNotMatched.INSERT))) + ).toCollection(); ```