From bb9b845ffde44f3d9220dd23e063ccbde6ab6260 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Wed, 17 Dec 2025 10:41:29 -0300 Subject: [PATCH 1/2] feat: Included new operators and added missing Java filters import --- docs/50-aggregation/2-match-project.mdx | 9 ++++-- docs/50-aggregation/3-sort-limit.mdx | 38 ++++++++++++++++--------- docs/50-aggregation/4-group.mdx | 2 ++ docs/50-aggregation/5-lookup.mdx | 2 ++ docs/50-aggregation/7-merge.mdx | 32 ++++++++++++--------- 5 files changed, 54 insertions(+), 29 deletions(-) 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..1908bd4 100644 --- a/docs/50-aggregation/4-group.mdx +++ b/docs/50-aggregation/4-group.mdx @@ -338,6 +338,8 @@ GROUP BY year;
```java + import static com.mongodb.client.model.Sorts.descending; + reviews.aggregate( List.of(Aggregates.group( "$name", diff --git a/docs/50-aggregation/5-lookup.mdx b/docs/50-aggregation/5-lookup.mdx index 432b913..7acaac0 100644 --- a/docs/50-aggregation/5-lookup.mdx +++ b/docs/50-aggregation/5-lookup.mdx @@ -168,6 +168,8 @@ The $lookup operation creates an array within each book document. Using $unwind
```java + import com.mongodb.client.model.Aggregates; + 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(); ``` From 48406961fd9b49090d2128c0e429be4f529a7101 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Wed, 17 Dec 2025 15:27:18 -0300 Subject: [PATCH 2/2] feat: pr review changes --- docs/50-aggregation/4-group.mdx | 8 +++++++- docs/50-aggregation/5-lookup.mdx | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/50-aggregation/4-group.mdx b/docs/50-aggregation/4-group.mdx index 1908bd4..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", @@ -339,7 +341,9 @@ GROUP BY year;
```java import static com.mongodb.client.model.Sorts.descending; - + + MongoCollection reviews = library.getCollection("reviews"); + reviews.aggregate( List.of(Aggregates.group( "$name", @@ -353,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 7acaac0..0f44541 100644 --- a/docs/50-aggregation/5-lookup.mdx +++ b/docs/50-aggregation/5-lookup.mdx @@ -170,6 +170,8 @@ 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(