Skip to content
Open
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>
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>