```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();
```