Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions docs/40-CRUD/1-WHERE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Now, translate the following into a MongoDB query.
}
```
</div>
</TabItem>

<TabItem value="python" label="Python">
<div>
```python
Expand All @@ -141,6 +141,26 @@ Now, translate the following into a MongoDB query.
print(f"Book Title: {title} - Total Inventory: {inventory}")
```
</div>
</TabItem>
</TabItem><TabItem value="java" label="Java">
<div>
```java
Bson filter = eq("totalInventory", 5);

Bson projection = Projections.fields(
Projections.include("title", "year", "totalInventory"));

List<Document> 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()));
}
```
</div>
</TabItem>
</Tabs>

Expand Down Expand Up @@ -185,7 +205,7 @@ Now, translate the following into a MongoDB query.
}
```
</div>
</TabItem>
</TabItem>
<TabItem value="python" label="Python">
<div>
```python
Expand All @@ -196,6 +216,26 @@ Now, translate the following into a MongoDB query.
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```java
Bson filter = gt("pages", 300);

Bson projection = Projections.fields(
Projections.include("title", "genres", "pages"));

List<Document> 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()));
}
```
</div>
</TabItem>
</Tabs>
</details>

Expand Down Expand Up @@ -246,8 +286,8 @@ Now, translate the following into a MongoDB query.
}
```
</div>
</TabItem>
<TabItem value="python" label="Python">
</TabItem>
<TabItem value="python" label="Python">
<div>
```python
books_with_genre_science_and_more_than_300_pages = books.find({"genres": "Science", "pages": {"$gt": 300}})
Expand All @@ -257,5 +297,27 @@ Now, translate the following into a MongoDB query.
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```java
Bson filter = and(
eq("genres", "Science"),
gt("pages", 300));

Bson projection = Projections.fields(
Projections.include("title", "genres", "pages"));

List<Document> 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()));
}
```
</div>
</TabItem>
</Tabs>
</details>
39 changes: 39 additions & 0 deletions docs/40-CRUD/2-SELECT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ Here:
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```java
Bson projection = Projections.fields(
Projections.include("title"),
Projections.exclude("_id"));

List<Document> 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()));
}
```
</div>
</TabItem>
</Tabs>
</details>

Expand Down Expand Up @@ -194,5 +213,25 @@ Here:
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```java
Bson filter = eq("genres", "History");
Bson projection = Projections.fields(
Projections.exclude("_id", "authors")
);

List<Document> 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()));
}
```
</div>
</TabItem>
</Tabs>
</details>
16 changes: 16 additions & 0 deletions docs/40-CRUD/3-ORDER-LIMIT.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,21 @@ This returns the **top 10 available books** in the "Science Fiction" genre.
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```java
List<Document> 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()));
}
```
</div>
</TabItem>
</Tabs>
</details>
45 changes: 44 additions & 1 deletion docs/40-CRUD/4-INSERT-DELETE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,34 @@ DELETE FROM reviews WHERE bookId = '0786222727';
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```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")));
```
</div>
</TabItem>
</Tabs>
</details>

Expand Down Expand Up @@ -225,6 +253,21 @@ DELETE FROM reviews WHERE bookId = '0786222727';
reviews.delete_many({"bookId": "0786222727"})
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```java
import static com.mongodb.client.model.Filters.eq;
import org.bson.conversions.Bson;

MongoCollection<Document> reviews = library.getCollection("reviews");

Bson filter = eq("bookId", "0786222727");

DeleteResult result = reviews.deleteMany(filter);
System.out.println(result.getDeletedCount() + " reviews deleted.");
```
</div>
</TabItem>
</Tabs>
</details>
</details>
15 changes: 14 additions & 1 deletion docs/40-CRUD/5-UPDATE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,18 @@ Executing the above command will insert a fresh new document in the collection,
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```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());
```
</div>
</TabItem>
</Tabs>
</details>
</details>
24 changes: 24 additions & 0 deletions docs/50-aggregation/2-match-project.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ db.books.aggregate([
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```java
books.aggregate(
List.of(
Aggregates.match(gt(
"available", 2)))
).forEach(document - > System.out.println(document.toJson()));
```
</div>
</TabItem>
</Tabs>
</details>

Expand Down Expand Up @@ -216,5 +227,18 @@ db.books.aggregate([
```
</div>
</TabItem>
<TabItem value="java" label="Java">
<div>
```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()));
```
</div>
</TabItem>
</Tabs>
</details>
52 changes: 52 additions & 0 deletions docs/50-aggregation/3-sort-limit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,58 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
</TabItem>
</Tabs>
</TabItem>
<TabItem value="java" label="Java">
<Tabs groupId="aggregations-java" defaultValue="addFields">
<TabItem value="project" label="Using $project">
<div>
```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()));
```
</div>
</TabItem>

<TabItem value="addFields" label="Using $addFields">
<div>
```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()));
```
</div>
</TabItem>
</Tabs>
</TabItem>
</Tabs>
</div>
</details>
Loading