Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/50-aggregation/2-match-project.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,13 @@ db.books.aggregate([
<TabItem value="java" label="Java">
<div>
```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()));
```
</div>
</TabItem>
Expand Down Expand Up @@ -230,13 +232,16 @@ db.books.aggregate([
<TabItem value="java" label="Java">
<div>
```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()));
```
</div>
</TabItem>
Expand Down
38 changes: 25 additions & 13 deletions docs/50-aggregation/3-sort-limit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
<TabItem value="project" label="Using $project">
<div>
```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)),
Expand All @@ -275,21 +281,27 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
<TabItem value="addFields" label="Using $addFields">
<div>
```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()));
```
Expand Down
8 changes: 8 additions & 0 deletions docs/50-aggregation/4-group.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ GROUP BY year;
<TabItem value="java" label="Java">
<div>
```java
MongoCollection<Document> reviews = library.getCollection("reviews");

reviews.aggregate(
List.of(
Aggregates.group("$bookId",
Expand Down Expand Up @@ -338,6 +340,10 @@ GROUP BY year;
<TabItem value="$group" label="$group with $sum + $sort">
<div>
```java
import static com.mongodb.client.model.Sorts.descending;

MongoCollection<Document> reviews = library.getCollection("reviews");

reviews.aggregate(
List.of(Aggregates.group(
"$name",
Expand All @@ -351,6 +357,8 @@ GROUP BY year;
<TabItem value="$sortByCount" label="$sortByCount">
<div>
```java
MongoCollection<Document> reviews = library.getCollection("reviews");

reviews.aggregate(
List.of(
Aggregates.sortByCount("$name"))
Expand Down
4 changes: 4 additions & 0 deletions docs/50-aggregation/5-lookup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ The $lookup operation creates an array within each book document. Using $unwind
<TabItem value="java" label="Java">
<div>
```java
import com.mongodb.client.model.Aggregates;

MongoCollection<Document> books = library.getCollection("books");

books.aggregate(
List.of(
Aggregates.lookup(
Expand Down
32 changes: 18 additions & 14 deletions docs/50-aggregation/7-merge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
<Tabs groupId="merge-java" defaultValue="books">
<TabItem value="books" label="through books collection">
```java
import com.mongodb.client.model.MergeOptions;

books.aggregate(
List.of(
Aggregates.unwind("$authors"),
Expand All @@ -250,20 +252,22 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
</TabItem>
<TabItem value="authors" label="through authors collection">
```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();
```
</TabItem>
</Tabs>
Expand Down