Skip to content

Commit f6c451d

Browse files
feat: Add aggregation examples using match/project, sort/limit, group and lookup
1 parent fd184a7 commit f6c451d

File tree

10 files changed

+238
-12
lines changed

10 files changed

+238
-12
lines changed

docs/40-CRUD/1-WHERE.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ Now, translate the following into a MongoDB query.
142142
```
143143
</div>
144144
</TabItem>
145-
</TabItem><TabItem value="Java" label="Java">
145+
</TabItem><TabItem value="java" label="Java">
146146
<div>
147-
```Java
147+
```java
148148
Bson filter = eq("totalInventory", 5);
149149

150150
Bson projection = Projections.fields(
@@ -216,9 +216,9 @@ Now, translate the following into a MongoDB query.
216216
```
217217
</div>
218218
</TabItem>
219-
<TabItem value="Java" label="Java">
219+
<TabItem value="java" label="Java">
220220
<div>
221-
```Java
221+
```java
222222
Bson filter = gt("pages", 300);
223223

224224
Bson projection = Projections.fields(
@@ -297,9 +297,9 @@ Now, translate the following into a MongoDB query.
297297
```
298298
</div>
299299
</TabItem>
300-
<TabItem value="Java" label="Java">
300+
<TabItem value="java" label="Java">
301301
<div>
302-
```Java
302+
```java
303303
Bson filter = and(
304304
eq("genres", "Science"),
305305
gt("pages", 300));

docs/40-CRUD/2-SELECT.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,25 @@ Here:
133133
```
134134
</div>
135135
</TabItem>
136+
<TabItem value="java" label="Java">
137+
<div>
138+
```java
139+
Bson projection = Projections.fields(
140+
Projections.include("title"),
141+
Projections.exclude("_id"));
142+
143+
List<Document> results = books.find()
144+
.projection(projection)
145+
.into(new ArrayList<>());
146+
147+
if (results.isEmpty()) {
148+
System.out.println("No books were found for the given query.");
149+
} else {
150+
results.forEach(doc -> System.out.println(doc.toJson()));
151+
}
152+
```
153+
</div>
154+
</TabItem>
136155
</Tabs>
137156
</details>
138157

@@ -194,5 +213,25 @@ Here:
194213
```
195214
</div>
196215
</TabItem>
216+
<TabItem value="java" label="Java">
217+
<div>
218+
```java
219+
Bson filter = eq("genres", "History");
220+
Bson projection = Projections.fields(
221+
Projections.exclude("_id", "authors")
222+
);
223+
224+
List<Document> results = books.find(filter)
225+
.projection(projection)
226+
.into(new ArrayList<>());
227+
228+
if (results.isEmpty()) {
229+
System.out.println("No books were found for the given query.");
230+
} else {
231+
results.forEach(doc -> System.out.println(doc.toJson()));
232+
}
233+
```
234+
</div>
235+
</TabItem>
197236
</Tabs>
198237
</details>

docs/40-CRUD/3-ORDER-LIMIT.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,21 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
109109
```
110110
</div>
111111
</TabItem>
112+
<TabItem value="java" label="Java">
113+
<div>
114+
```java
115+
List<Document> results = books.find()
116+
.sort(descending("title"))
117+
.limit(10)
118+
.into(new ArrayList<>());
119+
120+
if (results.isEmpty()) {
121+
System.out.println("No books were found for the given query.");
122+
} else {
123+
results.forEach(doc -> System.out.println(doc.toJson()));
124+
}
125+
```
126+
</div>
127+
</TabItem>
112128
</Tabs>
113129
</details>

docs/40-CRUD/4-INSERT-DELETE.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ DELETE FROM reviews WHERE bookId = '0786222727';
184184
```
185185
</div>
186186
</TabItem>
187-
<TabItem value="Java" label="Java">
187+
<TabItem value="java" label="Java">
188188
<div>
189-
```Java
189+
```java
190190
var reviews = library.getCollection("reviews");
191191

192192
reviews.insertMany(List.of(
@@ -254,9 +254,9 @@ DELETE FROM reviews WHERE bookId = '0786222727';
254254
```
255255
</div>
256256
</TabItem>
257-
<TabItem value="Java" label="Java">
257+
<TabItem value="java" label="Java">
258258
<div>
259-
```Java
259+
```java
260260
import static com.mongodb.client.model.Filters.eq;
261261
import org.bson.conversions.Bson;
262262

docs/40-CRUD/5-UPDATE.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ Executing the above command will insert a fresh new document in the collection,
130130
```
131131
</div>
132132
</TabItem>
133-
<TabItem value="Java" label="Java">
133+
<TabItem value="java" label="Java">
134134
<div>
135-
```Java
135+
```java
136136
import static com.mongodb.client.model.Filters.eq;
137137

138138
var query = eq("title", "Treasure of the Sun");

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ db.books.aggregate([
152152
```
153153
</div>
154154
</TabItem>
155+
<TabItem value="java" label="Java">
156+
<div>
157+
```java
158+
books.aggregate(
159+
List.of(
160+
Aggregates.match(gt(
161+
"available", 2)))
162+
).forEach(document - > System.out.println(document.toJson()));
163+
```
164+
</div>
165+
</TabItem>
155166
</Tabs>
156167
</details>
157168

@@ -216,5 +227,18 @@ db.books.aggregate([
216227
```
217228
</div>
218229
</TabItem>
230+
<TabItem value="java" label="Java">
231+
<div>
232+
```java
233+
books.aggregate(
234+
List.of(
235+
Aggregates.match(gt(
236+
"available", 2)),
237+
Aggregates.project(Projections.include("title", "year")),
238+
Aggregates.project(Projections.exclude("_id")))
239+
).forEach(document - > System.out.println(document.toJson()));
240+
```
241+
</div>
242+
</TabItem>
219243
</Tabs>
220244
</details>

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,58 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
245245
</TabItem>
246246
</Tabs>
247247
</TabItem>
248+
<TabItem value="java" label="Java">
249+
<Tabs groupId="aggregations-java" defaultValue="addFields">
250+
<TabItem value="project" label="Using $project">
251+
<div>
252+
```java
253+
books.aggregate(
254+
List.of(
255+
Aggregates.match(gt("year", 2000)),
256+
Aggregates.match(exists("authors", true)),
257+
Aggregates.project(
258+
Projections.fields(
259+
Projections.include("title", "year", "authors"),
260+
Projections.computed(
261+
"numAuthors",
262+
new Document("$size", "$authors")
263+
)
264+
)
265+
),
266+
267+
Aggregates.sort(Sorts.descending("numAuthors")),
268+
Aggregates.limit(1)
269+
)
270+
).forEach(c -> System.out.println(c.toJson()));
271+
```
272+
</div>
273+
</TabItem>
274+
275+
<TabItem value="addFields" label="Using $addFields">
276+
<div>
277+
```java
278+
books.aggregate(
279+
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)
293+
)
294+
).forEach(c -> System.out.println(c.toJson()));
295+
```
296+
</div>
297+
</TabItem>
298+
</Tabs>
299+
</TabItem>
248300
</Tabs>
249301
</div>
250302
</details>

docs/50-aggregation/4-group.mdx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ GROUP BY year;
191191
```
192192
</div>
193193
</TabItem>
194+
<TabItem value="java" label="Java">
195+
<div>
196+
```java
197+
reviews.aggregate(
198+
List.of(
199+
Aggregates.group("$bookId",
200+
Accumulators.avg("avgRating", "$rating")))
201+
).forEach(r -> System.out.println(r.toJson()));
202+
```
203+
</div>
204+
</TabItem>
194205
</Tabs>
195206
</details>
196207

@@ -322,6 +333,33 @@ GROUP BY year;
322333
</TabItem>
323334
</Tabs>
324335
</TabItem>
336+
<TabItem value="java" label="Java">
337+
<Tabs groupId="aggregations-java" defaultValue="$group">
338+
<TabItem value="$group" label="$group with $sum + $sort">
339+
<div>
340+
```java
341+
reviews.aggregate(
342+
List.of(Aggregates.group(
343+
"$name",
344+
Accumulators.sum("totalReviews", 1)),
345+
Aggregates.sort(descending("totalReviews")))
346+
).forEach(r -> System.out.println(r.toJson()));
347+
```
348+
</div>
349+
</TabItem>
350+
351+
<TabItem value="$sortByCount" label="$sortByCount">
352+
<div>
353+
```java
354+
reviews.aggregate(
355+
List.of(
356+
Aggregates.sortByCount("$name"))
357+
).forEach(r -> System.out.println(r.toJson()));
358+
```
359+
</div>
360+
</TabItem>
361+
</Tabs>
362+
</TabItem>
325363
</Tabs>
326364
</div>
327365
</details>

docs/50-aggregation/5-lookup.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,21 @@ The $lookup operation creates an array within each book document. Using $unwind
165165
```
166166
</div>
167167
</TabItem>
168+
<TabItem value="java" label="Java">
169+
<div>
170+
```java
171+
books.aggregate(
172+
List.of(
173+
Aggregates.lookup(
174+
"reviews",
175+
"_id",
176+
"bookId",
177+
"reviews"
178+
)
179+
)
180+
).forEach(r -> System.out.println(r.toJson()));
181+
```
182+
</div>
183+
</TabItem>
168184
</Tabs>
169185
</details>

docs/50-aggregation/7-merge.mdx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,47 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
227227
</TabItem>
228228
</Tabs>
229229
</TabItem>
230+
<TabItem value="java" label="Java">
231+
<Tabs groupId="merge-java" defaultValue="books">
232+
<TabItem value="books" label="through books collection">
233+
```java
234+
books.aggregate(
235+
List.of(
236+
Aggregates.unwind("$authors"),
237+
Aggregates.group(
238+
"$authors.name",
239+
Accumulators.sum("totalBooks", 1)
240+
),
241+
Aggregates.merge(
242+
"author_stats",
243+
new MergeOptions()
244+
.uniqueIdentifier("_id")
245+
.whenMatched(MergeOptions.WhenMatched.MERGE)
246+
.whenNotMatched(MergeOptions.WhenNotMatched.INSERT))
247+
)
248+
).toCollection();
249+
```
250+
</TabItem>
251+
<TabItem value="authors" label="through authors collection">
252+
```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();
267+
```
268+
</TabItem>
269+
</Tabs>
270+
</TabItem>
230271
</Tabs>
231272
</div>
232273
</details>

0 commit comments

Comments
 (0)