From 44a141686b010beb8cbccd7bd03817fc5a53f32f Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Thu, 11 Dec 2025 18:37:46 -0300 Subject: [PATCH 1/5] feat: Add Java implementations for find queries, including exact match, range filtering, and combined conditions --- docs/40-CRUD/1-WHERE.mdx | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/docs/40-CRUD/1-WHERE.mdx b/docs/40-CRUD/1-WHERE.mdx index 751bee8..ee54ef3 100644 --- a/docs/40-CRUD/1-WHERE.mdx +++ b/docs/40-CRUD/1-WHERE.mdx @@ -129,6 +129,25 @@ Now, translate the following into a MongoDB query. } ``` + +
+ ```Java + Bson filter = eq("totalInventory", 5); + + Bson projection = Projections.fields( + Projections.include("title", "year", "totalInventory")); + + List results = books.find(filter) + .projection(projection) + .into(new ArrayList<>()); + + if (results.isEmpty()) { + System.out.println("No books were found for the given query."); + } else { + results.forEach(doc -> System.out.println(doc.toJson())); + } + ``` +
@@ -173,6 +192,25 @@ Now, translate the following into a MongoDB query. } ``` + +
+ ```Java + Bson filter = gt("pages", 300); + + Bson projection = Projections.fields( + Projections.include("title", "genres", "pages")); + + List results = books.find(filter) + .projection(projection) + .into(new ArrayList<>()); + + if (results.isEmpty()) { + System.out.println("No books were found for the given query."); + } else { + results.forEach(doc -> System.out.println(doc.toJson())); + } + ``` +
@@ -224,6 +262,27 @@ Now, translate the following into a MongoDB query. } ``` + +
+ ```Java + Bson filter = and( + eq("genres", "Science"), + gt("pages", 300)); + + Bson projection = Projections.fields( + Projections.include("title", "genres", "pages")); + + List results = books.find(filter) + .projection(projection) + .into(new ArrayList<>()); + + if (results.isEmpty()) { + System.out.println("No books were found for the given query."); + } else { + results.forEach(doc -> System.out.println(doc.toJson())); + } + ``` +
From 3ede9b07756ac41d0febb0ff0734263c01e23957 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 12 Dec 2025 10:51:24 -0300 Subject: [PATCH 2/5] feat: Add Java examples demonstrating document insertion with basic and structured fields --- docs/40-CRUD/4-INSERT-DELETE.mdx | 43 +++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/40-CRUD/4-INSERT-DELETE.mdx b/docs/40-CRUD/4-INSERT-DELETE.mdx index c983d9b..348cceb 100644 --- a/docs/40-CRUD/4-INSERT-DELETE.mdx +++ b/docs/40-CRUD/4-INSERT-DELETE.mdx @@ -150,7 +150,34 @@ DELETE FROM reviews WHERE bookId = '0786222727'; reviewsCollection.InsertMany(newReviews); ``` - + +
+ ```Java + var reviews = library.getCollection("reviews"); + + reviews.insertMany(List.of( + new Document("text", "Thrilling end.") + .append("rating", 4) + .append("name", "Mark") + .append("bookId", "0786222727"), + + new Document("text", "Very expensive") + .append("rating", 3) + .append("name", "Yun") + .append("bookId", "0786222727"), + + new Document("text", "Must read!.") + .append("rating", 6) + .append("name", "Raj") + .append("bookId", "0786222727"), + + new Document("text", "Extremely satisfied with the storyline!") + .append("rating", 5) + .append("name", "Lisa") + .append("bookId", "0786222727"))); + ``` +
+
@@ -184,6 +211,20 @@ DELETE FROM reviews WHERE bookId = '0786222727'; Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted."); ``` + +
+ ```Java + import static com.mongodb.client.model.Filters.eq; + import org.bson.conversions.Bson; + + MongoCollection reviews = library.getCollection("reviews"); + + Bson filter = eq("bookId", "0786222727"); + + DeleteResult result = reviews.deleteMany(filter); + System.out.println(result.getDeletedCount() + " reviews deleted."); + ``` +
From bafe0665cec22072faaa0c57b448793040272403 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Fri, 12 Dec 2025 10:51:42 -0300 Subject: [PATCH 3/5] feat: Implement Java update operations --- docs/40-CRUD/5-UPDATE.mdx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/40-CRUD/5-UPDATE.mdx b/docs/40-CRUD/5-UPDATE.mdx index f92d422..22ebfd2 100644 --- a/docs/40-CRUD/5-UPDATE.mdx +++ b/docs/40-CRUD/5-UPDATE.mdx @@ -118,6 +118,17 @@ Executing the above command will insert a fresh new document in the collection, Console.WriteLine($"Matched: {result.MatchedCount}, Modified: {result.ModifiedCount}"); ``` + +
+ ```Java + import static com.mongodb.client.model.Filters.eq; + + var query = eq("title", "Treasure of the Sun"); + var update = Updates.set("pages", 449); + UpdateResult updateResult = books.updateOne(query, update); + System.out.println("Modified document count: " + updateResult.getModifiedCount()); + ``` +
From fd184a77424dddf66957c014ab9e9ba24533956a Mon Sep 17 00:00:00 2001 From: Diego Freniche Date: Mon, 15 Dec 2025 13:52:29 +0100 Subject: [PATCH 4/5] Added Java and SQL language syntax coloring, removed Swift and Kotlin --- docusaurus.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index 3f12e20..bb9ffc9 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -152,7 +152,7 @@ const config = { prism: { theme: lightCodeTheme, darkTheme: darkCodeTheme, - additionalLanguages: ["powershell", "swift", "kotlin", "python", "csharp"], + additionalLanguages: ["powershell", "python", "csharp", "java", "sql"], }, mermaid: { theme: { light: "neutral", dark: "forest" }, From f6c451df95b326446eae4a2a16f49e54d6d52ba0 Mon Sep 17 00:00:00 2001 From: Ricardo Mello Date: Mon, 15 Dec 2025 18:33:16 -0300 Subject: [PATCH 5/5] feat: Add aggregation examples using match/project, sort/limit, group and lookup --- docs/40-CRUD/1-WHERE.mdx | 12 +++--- docs/40-CRUD/2-SELECT.mdx | 39 +++++++++++++++++++ docs/40-CRUD/3-ORDER-LIMIT.mdx | 16 ++++++++ docs/40-CRUD/4-INSERT-DELETE.mdx | 8 ++-- docs/40-CRUD/5-UPDATE.mdx | 4 +- docs/50-aggregation/2-match-project.mdx | 24 ++++++++++++ docs/50-aggregation/3-sort-limit.mdx | 52 +++++++++++++++++++++++++ docs/50-aggregation/4-group.mdx | 38 ++++++++++++++++++ docs/50-aggregation/5-lookup.mdx | 16 ++++++++ docs/50-aggregation/7-merge.mdx | 41 +++++++++++++++++++ 10 files changed, 238 insertions(+), 12 deletions(-) diff --git a/docs/40-CRUD/1-WHERE.mdx b/docs/40-CRUD/1-WHERE.mdx index 2c006f6..425a042 100644 --- a/docs/40-CRUD/1-WHERE.mdx +++ b/docs/40-CRUD/1-WHERE.mdx @@ -142,9 +142,9 @@ Now, translate the following into a MongoDB query. ``` - +
- ```Java + ```java Bson filter = eq("totalInventory", 5); Bson projection = Projections.fields( @@ -216,9 +216,9 @@ Now, translate the following into a MongoDB query. ```
- +
- ```Java + ```java Bson filter = gt("pages", 300); Bson projection = Projections.fields( @@ -297,9 +297,9 @@ Now, translate the following into a MongoDB query. ```
- +
- ```Java + ```java Bson filter = and( eq("genres", "Science"), gt("pages", 300)); diff --git a/docs/40-CRUD/2-SELECT.mdx b/docs/40-CRUD/2-SELECT.mdx index e3fe866..8fa26b5 100644 --- a/docs/40-CRUD/2-SELECT.mdx +++ b/docs/40-CRUD/2-SELECT.mdx @@ -133,6 +133,25 @@ Here: ```
+ +
+ ```java + Bson projection = Projections.fields( + Projections.include("title"), + Projections.exclude("_id")); + + List results = books.find() + .projection(projection) + .into(new ArrayList<>()); + + if (results.isEmpty()) { + System.out.println("No books were found for the given query."); + } else { + results.forEach(doc -> System.out.println(doc.toJson())); + } + ``` +
+
@@ -194,5 +213,25 @@ Here: ```
+ +
+ ```java + Bson filter = eq("genres", "History"); + Bson projection = Projections.fields( + Projections.exclude("_id", "authors") + ); + + List results = books.find(filter) + .projection(projection) + .into(new ArrayList<>()); + + if (results.isEmpty()) { + System.out.println("No books were found for the given query."); + } else { + results.forEach(doc -> System.out.println(doc.toJson())); + } + ``` +
+
diff --git a/docs/40-CRUD/3-ORDER-LIMIT.mdx b/docs/40-CRUD/3-ORDER-LIMIT.mdx index 534588e..3da39b0 100644 --- a/docs/40-CRUD/3-ORDER-LIMIT.mdx +++ b/docs/40-CRUD/3-ORDER-LIMIT.mdx @@ -109,5 +109,21 @@ This returns the **top 10 available books** in the "Science Fiction" genre. ```
+ +
+ ```java + List results = books.find() + .sort(descending("title")) + .limit(10) + .into(new ArrayList<>()); + + if (results.isEmpty()) { + System.out.println("No books were found for the given query."); + } else { + results.forEach(doc -> System.out.println(doc.toJson())); + } + ``` +
+
diff --git a/docs/40-CRUD/4-INSERT-DELETE.mdx b/docs/40-CRUD/4-INSERT-DELETE.mdx index de71454..cf861b8 100644 --- a/docs/40-CRUD/4-INSERT-DELETE.mdx +++ b/docs/40-CRUD/4-INSERT-DELETE.mdx @@ -184,9 +184,9 @@ DELETE FROM reviews WHERE bookId = '0786222727'; ``` - +
- ```Java + ```java var reviews = library.getCollection("reviews"); reviews.insertMany(List.of( @@ -254,9 +254,9 @@ DELETE FROM reviews WHERE bookId = '0786222727'; ```
- +
- ```Java + ```java import static com.mongodb.client.model.Filters.eq; import org.bson.conversions.Bson; diff --git a/docs/40-CRUD/5-UPDATE.mdx b/docs/40-CRUD/5-UPDATE.mdx index c079fe2..a3ff9d7 100644 --- a/docs/40-CRUD/5-UPDATE.mdx +++ b/docs/40-CRUD/5-UPDATE.mdx @@ -130,9 +130,9 @@ Executing the above command will insert a fresh new document in the collection, ```
- +
- ```Java + ```java import static com.mongodb.client.model.Filters.eq; var query = eq("title", "Treasure of the Sun"); diff --git a/docs/50-aggregation/2-match-project.mdx b/docs/50-aggregation/2-match-project.mdx index b437b48..8c32db6 100644 --- a/docs/50-aggregation/2-match-project.mdx +++ b/docs/50-aggregation/2-match-project.mdx @@ -152,6 +152,17 @@ db.books.aggregate([ ```
+ +
+ ```java + books.aggregate( + List.of( + Aggregates.match(gt( + "available", 2))) + ).forEach(document - > System.out.println(document.toJson())); + ``` +
+
@@ -216,5 +227,18 @@ db.books.aggregate([ ```
+ +
+ ```java + 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())); + ``` +
+
diff --git a/docs/50-aggregation/3-sort-limit.mdx b/docs/50-aggregation/3-sort-limit.mdx index 6924134..ada9a51 100644 --- a/docs/50-aggregation/3-sort-limit.mdx +++ b/docs/50-aggregation/3-sort-limit.mdx @@ -245,6 +245,58 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
+ + + +
+ ```java + books.aggregate( + List.of( + Aggregates.match(gt("year", 2000)), + Aggregates.match(exists("authors", true)), + Aggregates.project( + Projections.fields( + Projections.include("title", "year", "authors"), + Projections.computed( + "numAuthors", + new Document("$size", "$authors") + ) + ) + ), + + Aggregates.sort(Sorts.descending("numAuthors")), + Aggregates.limit(1) + ) + ).forEach(c -> System.out.println(c.toJson())); + ``` +
+
+ + +
+ ```java + 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) + ) + ).forEach(c -> System.out.println(c.toJson())); + ``` +
+
+
+
diff --git a/docs/50-aggregation/4-group.mdx b/docs/50-aggregation/4-group.mdx index 98f85fe..6e163af 100644 --- a/docs/50-aggregation/4-group.mdx +++ b/docs/50-aggregation/4-group.mdx @@ -191,6 +191,17 @@ GROUP BY year; ``` + +
+ ```java + reviews.aggregate( + List.of( + Aggregates.group("$bookId", + Accumulators.avg("avgRating", "$rating"))) + ).forEach(r -> System.out.println(r.toJson())); + ``` +
+
@@ -322,6 +333,33 @@ GROUP BY year; + + + +
+ ```java + reviews.aggregate( + List.of(Aggregates.group( + "$name", + Accumulators.sum("totalReviews", 1)), + Aggregates.sort(descending("totalReviews"))) + ).forEach(r -> System.out.println(r.toJson())); + ``` +
+
+ + +
+ ```java + reviews.aggregate( + List.of( + Aggregates.sortByCount("$name")) + ).forEach(r -> System.out.println(r.toJson())); + ``` +
+
+
+
diff --git a/docs/50-aggregation/5-lookup.mdx b/docs/50-aggregation/5-lookup.mdx index 369a91e..432b913 100644 --- a/docs/50-aggregation/5-lookup.mdx +++ b/docs/50-aggregation/5-lookup.mdx @@ -165,5 +165,21 @@ The $lookup operation creates an array within each book document. Using $unwind ``` + +
+ ```java + books.aggregate( + List.of( + Aggregates.lookup( + "reviews", + "_id", + "bookId", + "reviews" + ) + ) + ).forEach(r -> System.out.println(r.toJson())); + ``` +
+
diff --git a/docs/50-aggregation/7-merge.mdx b/docs/50-aggregation/7-merge.mdx index 3788bb5..ac29039 100644 --- a/docs/50-aggregation/7-merge.mdx +++ b/docs/50-aggregation/7-merge.mdx @@ -227,6 +227,47 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks); + + + + ```java + books.aggregate( + List.of( + Aggregates.unwind("$authors"), + Aggregates.group( + "$authors.name", + Accumulators.sum("totalBooks", 1) + ), + Aggregates.merge( + "author_stats", + new MergeOptions() + .uniqueIdentifier("_id") + .whenMatched(MergeOptions.WhenMatched.MERGE) + .whenNotMatched(MergeOptions.WhenNotMatched.INSERT)) + ) + ).toCollection(); + ``` + + + ```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(); + ``` + + +