Skip to content

Commit 87d93b2

Browse files
Merge pull request #34 from mongodb-developer/add-java-code
Add java code
2 parents 7d02a3d + f6c451d commit 87d93b2

File tree

11 files changed

+351
-7
lines changed

11 files changed

+351
-7
lines changed

docs/40-CRUD/1-WHERE.mdx

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Now, translate the following into a MongoDB query.
129129
}
130130
```
131131
</div>
132-
</TabItem>
132+
133133
<TabItem value="python" label="Python">
134134
<div>
135135
```python
@@ -141,6 +141,26 @@ Now, translate the following into a MongoDB query.
141141
print(f"Book Title: {title} - Total Inventory: {inventory}")
142142
```
143143
</div>
144+
</TabItem>
145+
</TabItem><TabItem value="java" label="Java">
146+
<div>
147+
```java
148+
Bson filter = eq("totalInventory", 5);
149+
150+
Bson projection = Projections.fields(
151+
Projections.include("title", "year", "totalInventory"));
152+
153+
List<Document> results = books.find(filter)
154+
.projection(projection)
155+
.into(new ArrayList<>());
156+
157+
if (results.isEmpty()) {
158+
System.out.println("No books were found for the given query.");
159+
} else {
160+
results.forEach(doc -> System.out.println(doc.toJson()));
161+
}
162+
```
163+
</div>
144164
</TabItem>
145165
</Tabs>
146166

@@ -185,7 +205,7 @@ Now, translate the following into a MongoDB query.
185205
}
186206
```
187207
</div>
188-
</TabItem>
208+
</TabItem>
189209
<TabItem value="python" label="Python">
190210
<div>
191211
```python
@@ -196,6 +216,26 @@ Now, translate the following into a MongoDB query.
196216
```
197217
</div>
198218
</TabItem>
219+
<TabItem value="java" label="Java">
220+
<div>
221+
```java
222+
Bson filter = gt("pages", 300);
223+
224+
Bson projection = Projections.fields(
225+
Projections.include("title", "genres", "pages"));
226+
227+
List<Document> results = books.find(filter)
228+
.projection(projection)
229+
.into(new ArrayList<>());
230+
231+
if (results.isEmpty()) {
232+
System.out.println("No books were found for the given query.");
233+
} else {
234+
results.forEach(doc -> System.out.println(doc.toJson()));
235+
}
236+
```
237+
</div>
238+
</TabItem>
199239
</Tabs>
200240
</details>
201241

@@ -246,8 +286,8 @@ Now, translate the following into a MongoDB query.
246286
}
247287
```
248288
</div>
249-
</TabItem>
250-
<TabItem value="python" label="Python">
289+
</TabItem>
290+
<TabItem value="python" label="Python">
251291
<div>
252292
```python
253293
books_with_genre_science_and_more_than_300_pages = books.find({"genres": "Science", "pages": {"$gt": 300}})
@@ -257,5 +297,27 @@ Now, translate the following into a MongoDB query.
257297
```
258298
</div>
259299
</TabItem>
300+
<TabItem value="java" label="Java">
301+
<div>
302+
```java
303+
Bson filter = and(
304+
eq("genres", "Science"),
305+
gt("pages", 300));
306+
307+
Bson projection = Projections.fields(
308+
Projections.include("title", "genres", "pages"));
309+
310+
List<Document> results = books.find(filter)
311+
.projection(projection)
312+
.into(new ArrayList<>());
313+
314+
if (results.isEmpty()) {
315+
System.out.println("No books were found for the given query.");
316+
} else {
317+
results.forEach(doc -> System.out.println(doc.toJson()));
318+
}
319+
```
320+
</div>
321+
</TabItem>
260322
</Tabs>
261323
</details>

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: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,34 @@ DELETE FROM reviews WHERE bookId = '0786222727';
184184
```
185185
</div>
186186
</TabItem>
187+
<TabItem value="java" label="Java">
188+
<div>
189+
```java
190+
var reviews = library.getCollection("reviews");
191+
192+
reviews.insertMany(List.of(
193+
new Document("text", "Thrilling end.")
194+
.append("rating", 4)
195+
.append("name", "Mark")
196+
.append("bookId", "0786222727"),
197+
198+
new Document("text", "Very expensive")
199+
.append("rating", 3)
200+
.append("name", "Yun")
201+
.append("bookId", "0786222727"),
202+
203+
new Document("text", "Must read!.")
204+
.append("rating", 6)
205+
.append("name", "Raj")
206+
.append("bookId", "0786222727"),
207+
208+
new Document("text", "Extremely satisfied with the storyline!")
209+
.append("rating", 5)
210+
.append("name", "Lisa")
211+
.append("bookId", "0786222727")));
212+
```
213+
</div>
214+
</TabItem>
187215
</Tabs>
188216
</details>
189217

@@ -225,6 +253,21 @@ DELETE FROM reviews WHERE bookId = '0786222727';
225253
reviews.delete_many({"bookId": "0786222727"})
226254
```
227255
</div>
256+
</TabItem>
257+
<TabItem value="java" label="Java">
258+
<div>
259+
```java
260+
import static com.mongodb.client.model.Filters.eq;
261+
import org.bson.conversions.Bson;
262+
263+
MongoCollection<Document> reviews = library.getCollection("reviews");
264+
265+
Bson filter = eq("bookId", "0786222727");
266+
267+
DeleteResult result = reviews.deleteMany(filter);
268+
System.out.println(result.getDeletedCount() + " reviews deleted.");
269+
```
270+
</div>
228271
</TabItem>
229272
</Tabs>
230-
</details>
273+
</details>

docs/40-CRUD/5-UPDATE.mdx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,18 @@ Executing the above command will insert a fresh new document in the collection,
130130
```
131131
</div>
132132
</TabItem>
133+
<TabItem value="java" label="Java">
134+
<div>
135+
```java
136+
import static com.mongodb.client.model.Filters.eq;
137+
138+
var query = eq("title", "Treasure of the Sun");
139+
var update = Updates.set("pages", 449);
140+
141+
UpdateResult updateResult = books.updateOne(query, update);
142+
System.out.println("Modified document count: " + updateResult.getModifiedCount());
143+
```
144+
</div>
145+
</TabItem>
133146
</Tabs>
134-
</details>
147+
</details>

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>

0 commit comments

Comments
 (0)