Skip to content

Commit 44a1416

Browse files
feat: Add Java implementations for find queries, including exact match, range filtering, and combined conditions
1 parent 7bf73fd commit 44a1416

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

docs/40-CRUD/1-WHERE.mdx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ Now, translate the following into a MongoDB query.
129129
}
130130
```
131131
</div>
132+
</TabItem><TabItem value="Java" label="Java">
133+
<div>
134+
```Java
135+
Bson filter = eq("totalInventory", 5);
136+
137+
Bson projection = Projections.fields(
138+
Projections.include("title", "year", "totalInventory"));
139+
140+
List<Document> results = books.find(filter)
141+
.projection(projection)
142+
.into(new ArrayList<>());
143+
144+
if (results.isEmpty()) {
145+
System.out.println("No books were found for the given query.");
146+
} else {
147+
results.forEach(doc -> System.out.println(doc.toJson()));
148+
}
149+
```
150+
</div>
132151
</TabItem>
133152
</Tabs>
134153

@@ -173,6 +192,25 @@ Now, translate the following into a MongoDB query.
173192
}
174193
```
175194
</div>
195+
</TabItem><TabItem value="Java" label="Java">
196+
<div>
197+
```Java
198+
Bson filter = gt("pages", 300);
199+
200+
Bson projection = Projections.fields(
201+
Projections.include("title", "genres", "pages"));
202+
203+
List<Document> results = books.find(filter)
204+
.projection(projection)
205+
.into(new ArrayList<>());
206+
207+
if (results.isEmpty()) {
208+
System.out.println("No books were found for the given query.");
209+
} else {
210+
results.forEach(doc -> System.out.println(doc.toJson()));
211+
}
212+
```
213+
</div>
176214
</TabItem>
177215
</Tabs>
178216
</details>
@@ -224,6 +262,27 @@ Now, translate the following into a MongoDB query.
224262
}
225263
```
226264
</div>
265+
</TabItem><TabItem value="Java" label="Java">
266+
<div>
267+
```Java
268+
Bson filter = and(
269+
eq("genres", "Science"),
270+
gt("pages", 300));
271+
272+
Bson projection = Projections.fields(
273+
Projections.include("title", "genres", "pages"));
274+
275+
List<Document> results = books.find(filter)
276+
.projection(projection)
277+
.into(new ArrayList<>());
278+
279+
if (results.isEmpty()) {
280+
System.out.println("No books were found for the given query.");
281+
} else {
282+
results.forEach(doc -> System.out.println(doc.toJson()));
283+
}
284+
```
285+
</div>
227286
</TabItem>
228287
</Tabs>
229288
</details>

0 commit comments

Comments
 (0)