Skip to content

Commit edb7496

Browse files
Merge pull request #36 from mongodb-developer/new-java-feat
feat: Included new operators and added missing Java filters import
2 parents 9d1f1dd + 4840696 commit edb7496

File tree

5 files changed

+62
-29
lines changed

5 files changed

+62
-29
lines changed

docs/50-aggregation/2-match-project.mdx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ db.books.aggregate([
155155
<TabItem value="java" label="Java">
156156
<div>
157157
```java
158+
import com.mongodb.client.model.Aggregates;
159+
158160
books.aggregate(
159161
List.of(
160162
Aggregates.match(gt(
161163
"available", 2)))
162-
).forEach(document - > System.out.println(document.toJson()));
164+
).forEach(document -> System.out.println(document.toJson()));
163165
```
164166
</div>
165167
</TabItem>
@@ -230,13 +232,16 @@ db.books.aggregate([
230232
<TabItem value="java" label="Java">
231233
<div>
232234
```java
235+
import com.mongodb.client.model.Aggregates;
236+
import com.mongodb.client.model.Projections;
237+
233238
books.aggregate(
234239
List.of(
235240
Aggregates.match(gt(
236241
"available", 2)),
237242
Aggregates.project(Projections.include("title", "year")),
238243
Aggregates.project(Projections.exclude("_id")))
239-
).forEach(document - > System.out.println(document.toJson()));
244+
).forEach(document -> System.out.println(document.toJson()));
240245
```
241246
</div>
242247
</TabItem>

docs/50-aggregation/3-sort-limit.mdx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
250250
<TabItem value="project" label="Using $project">
251251
<div>
252252
```java
253+
import com.mongodb.client.model.Projections;
254+
import com.mongodb.client.model.Field;
255+
import com.mongodb.client.model.Sorts;
256+
import static com.mongodb.client.model.Filters.exists;
257+
import static com.mongodb.client.model.Filters.gt;
258+
253259
books.aggregate(
254260
List.of(
255261
Aggregates.match(gt("year", 2000)),
@@ -275,21 +281,27 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
275281
<TabItem value="addFields" label="Using $addFields">
276282
<div>
277283
```java
284+
import com.mongodb.client.model.Projections;
285+
import com.mongodb.client.model.Field;
286+
import com.mongodb.client.model.Sorts;
287+
import static com.mongodb.client.model.Filters.exists;
288+
import static com.mongodb.client.model.Filters.gt;
289+
278290
books.aggregate(
279291
List.of(
280-
Aggregates.match(gt("year", 2000)),
281-
Aggregates.match(exists("authors", true)),
282-
Aggregates.addFields(
283-
new Field<>(
284-
"numAuthors",
285-
new Document("$size", "$authors")
286-
)
287-
),
288-
Aggregates.project(
289-
Projections.fields(
290-
Projections.include("title", "year", "authors", "numAuthors"))),
291-
Aggregates.sort(Sorts.descending("numAuthors")),
292-
Aggregates.limit(1)
292+
Aggregates.match(gt("year", 2000)),
293+
Aggregates.match(exists("authors", true)),
294+
Aggregates.addFields(
295+
new Field<>(
296+
"numAuthors",
297+
new Document("$size", "$authors")
298+
)
299+
),
300+
Aggregates.project(
301+
Projections.fields(
302+
Projections.include("title", "year", "authors", "numAuthors"))),
303+
Aggregates.sort(Sorts.descending("numAuthors")),
304+
Aggregates.limit(1)
293305
)
294306
).forEach(c -> System.out.println(c.toJson()));
295307
```

docs/50-aggregation/4-group.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ GROUP BY year;
194194
<TabItem value="java" label="Java">
195195
<div>
196196
```java
197+
MongoCollection<Document> reviews = library.getCollection("reviews");
198+
197199
reviews.aggregate(
198200
List.of(
199201
Aggregates.group("$bookId",
@@ -338,6 +340,10 @@ GROUP BY year;
338340
<TabItem value="$group" label="$group with $sum + $sort">
339341
<div>
340342
```java
343+
import static com.mongodb.client.model.Sorts.descending;
344+
345+
MongoCollection<Document> reviews = library.getCollection("reviews");
346+
341347
reviews.aggregate(
342348
List.of(Aggregates.group(
343349
"$name",
@@ -351,6 +357,8 @@ GROUP BY year;
351357
<TabItem value="$sortByCount" label="$sortByCount">
352358
<div>
353359
```java
360+
MongoCollection<Document> reviews = library.getCollection("reviews");
361+
354362
reviews.aggregate(
355363
List.of(
356364
Aggregates.sortByCount("$name"))

docs/50-aggregation/5-lookup.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ The $lookup operation creates an array within each book document. Using $unwind
168168
<TabItem value="java" label="Java">
169169
<div>
170170
```java
171+
import com.mongodb.client.model.Aggregates;
172+
173+
MongoCollection<Document> books = library.getCollection("books");
174+
171175
books.aggregate(
172176
List.of(
173177
Aggregates.lookup(

docs/50-aggregation/7-merge.mdx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
231231
<Tabs groupId="merge-java" defaultValue="books">
232232
<TabItem value="books" label="through books collection">
233233
```java
234+
import com.mongodb.client.model.MergeOptions;
235+
234236
books.aggregate(
235237
List.of(
236238
Aggregates.unwind("$authors"),
@@ -250,20 +252,22 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
250252
</TabItem>
251253
<TabItem value="authors" label="through authors collection">
252254
```java
253-
authors.aggregate(
254-
List.of(
255-
Aggregates.unwind("$books"),
256-
Aggregates.group(
257-
"$name",
258-
Accumulators.sum("totalBooks", 1)
259-
),
260-
Aggregates.merge(
261-
"author_stats",
262-
new MergeOptions()
263-
.uniqueIdentifier("_id")
264-
.whenMatched(MergeOptions.WhenMatched.MERGE)
265-
.whenNotMatched(MergeOptions.WhenNotMatched.INSERT)))
266-
).toCollection();
255+
import com.mongodb.client.model.MergeOptions;
256+
257+
authors.aggregate(
258+
List.of(
259+
Aggregates.unwind("$books"),
260+
Aggregates.group(
261+
"$name",
262+
Accumulators.sum("totalBooks", 1)
263+
),
264+
Aggregates.merge(
265+
"author_stats",
266+
new MergeOptions()
267+
.uniqueIdentifier("_id")
268+
.whenMatched(MergeOptions.WhenMatched.MERGE)
269+
.whenNotMatched(MergeOptions.WhenNotMatched.INSERT)))
270+
).toCollection();
267271
```
268272
</TabItem>
269273
</Tabs>

0 commit comments

Comments
 (0)