diff --git a/docs/40-CRUD/1-WHERE.mdx b/docs/40-CRUD/1-WHERE.mdx index 325b053..425a042 100644 --- a/docs/40-CRUD/1-WHERE.mdx +++ b/docs/40-CRUD/1-WHERE.mdx @@ -129,7 +129,7 @@ Now, translate the following into a MongoDB query. } ``` - +
```python @@ -141,6 +141,26 @@ Now, translate the following into a MongoDB query. print(f"Book Title: {title} - Total Inventory: {inventory}") ```
+
+ +
+ ```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())); + } + ``` +
@@ -185,7 +205,7 @@ Now, translate the following into a MongoDB query. } ``` - +
```python @@ -196,6 +216,26 @@ 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())); + } + ``` +
+
@@ -246,8 +286,8 @@ Now, translate the following into a MongoDB query. } ``` - - + +
```python 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. ```
+ +
+ ```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())); + } + ``` +
+
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 0af14fc..cf861b8 100644 --- a/docs/40-CRUD/4-INSERT-DELETE.mdx +++ b/docs/40-CRUD/4-INSERT-DELETE.mdx @@ -184,6 +184,34 @@ DELETE FROM reviews WHERE bookId = '0786222727'; ``` + +
+ ```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"))); + ``` +
+
@@ -225,6 +253,21 @@ DELETE FROM reviews WHERE bookId = '0786222727'; reviews.delete_many({"bookId": "0786222727"}) ``` + + +
+ ```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."); + ``` +
- + \ No newline at end of file diff --git a/docs/40-CRUD/5-UPDATE.mdx b/docs/40-CRUD/5-UPDATE.mdx index f25f45f..a3ff9d7 100644 --- a/docs/40-CRUD/5-UPDATE.mdx +++ b/docs/40-CRUD/5-UPDATE.mdx @@ -130,5 +130,18 @@ Executing the above command will insert a fresh new document in the collection, ``` + +
+ ```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()); + ``` +
+
- + \ No newline at end of file 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(); + ``` + + + 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" },