Skip to content

Commit 4a64684

Browse files
Improve Delete CRUD section and exercises
- Added introductory explanations for delete operations - Added examples using deleteOne() and deleteMany() - Improved delete challenge instructions and hints - Refined filter and variable naming for better readability - Added MongoDB documentation links with UTM tracking - Added explanations about DeleteResult and deleted document counts
1 parent 770dae1 commit 4a64684

1 file changed

Lines changed: 52 additions & 14 deletions

File tree

java/40_delete.ipynb

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
"id": "3b936925-e295-489a-b508-2b99c0160217",
1414
"metadata": {},
1515
"source": [
16-
"# CRUD: Delete\n",
17-
" "
16+
"# Basic CRUD: Delete\n",
17+
"\n",
18+
"## What can the `delete` operation do?\n",
19+
"\n",
20+
"The [`delete`](https://www.mongodb.com/docs/drivers/java/sync/current/crud/delete/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) operations let us remove documents from a MongoDB collection.\n",
21+
"\n",
22+
"In this section, we will use operations such as `deleteOne()` and `deleteMany()` to remove documents from the `books` collection."
1823
]
1924
},
2025
{
@@ -54,6 +59,10 @@
5459
"\n",
5560
"import java.util.List;\n",
5661
"\n",
62+
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
63+
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
64+
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
65+
"\n",
5766
"// Set your connection String\n",
5867
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
5968
"\n",
@@ -77,7 +86,7 @@
7786
"source": [
7887
"## Insert one book\n",
7988
"\n",
80-
"We want to make sure this book is in the collection, so we can later delete it."
89+
"We want to make sure this book exists in the collection so we can later remove it using a delete operation."
8190
]
8291
},
8392
{
@@ -91,12 +100,10 @@
91100
},
92101
"outputs": [],
93102
"source": [
94-
"Document platero = new Document();\n",
95-
"\n",
96-
"platero\n",
97-
" .append(\"_id\", \"platero\")\n",
98-
" .append(\"title\", \"Platero y yo\")\n",
99-
" .append(\"year\", 1900);\n",
103+
"Document platero = new Document()\n",
104+
" .append(\"_id\", \"platero\")\n",
105+
" .append(\"title\", \"Platero y yo\")\n",
106+
" .append(\"year\", 1900);\n",
100107
"\n",
101108
"books.insertOne(platero);"
102109
]
@@ -106,7 +113,9 @@
106113
"id": "9f6caab3",
107114
"metadata": {},
108115
"source": [
109-
"### We find the document we just inserted using the `_id` field"
116+
"### Find the inserted book\n",
117+
"\n",
118+
"In this example, we query the `books` collection using the `_id` field to confirm that the `Platero y yo` document exists before deleting it."
110119
]
111120
},
112121
{
@@ -131,7 +140,11 @@
131140
"id": "worth-windows",
132141
"metadata": {},
133142
"source": [
134-
"### Delete that book"
143+
"### Delete one book\n",
144+
"\n",
145+
"In this example, we use `deleteOne()` to remove the document with `_id` equal to `\"platero\"` from the `books` collection. \n",
146+
"\n",
147+
"The returned `DeleteResult` lets us verify how many documents were deleted."
135148
]
136149
},
137150
{
@@ -145,12 +158,13 @@
145158
},
146159
"outputs": [],
147160
"source": [
148-
"Document bookFrom1500 = new Document(\"year\", 1500);\n",
161+
"Document plateroFilter = new Document(\"_id\", \"platero\");\n",
149162
"\n",
150-
"DeleteResult deleteResult = books.deleteOne(new Document(\"_id\", \"platero\"));\n",
163+
"DeleteResult deleteResult = books.deleteOne(plateroFilter);\n",
151164
"\n",
152165
"// Check the number of deleted documents\n",
153166
"long deletedCount = deleteResult.getDeletedCount();\n",
167+
"\n",
154168
"System.out.println(\"Deleted \" + deletedCount + \" document(s).\");"
155169
]
156170
},
@@ -169,6 +183,10 @@
169183
"source": [
170184
"### Delete all the reviews for bookId \"0786222727\" through a single command.\n",
171185
"\n",
186+
"\n",
187+
"_Hint: You may need an operation capable of deleting multiple documents at once._\n",
188+
"\n",
189+
"\n",
172190
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/INSERT-DELETE#-2-delete-all-the-reviews-for-bookid-0786222727-through-a-single-command)"
173191
]
174192
},
@@ -183,7 +201,27 @@
183201
},
184202
"outputs": [],
185203
"source": [
186-
"// type your code here\n"
204+
"//TYPE YOUR CODE HERE\n",
205+
"\n",
206+
"MongoCollection<Document> reviews = library.getCollection(\"reviews\");\n",
207+
"\n",
208+
"Bson filter = <REPLACE_WITH_FILTER>;\n",
209+
"\n",
210+
"DeleteResult result = <REPLACE_WITH_DELETE_OPERATION>;\n",
211+
"\n",
212+
"System.out.println(result.getDeletedCount() + \" reviews deleted.\");"
213+
]
214+
},
215+
{
216+
"cell_type": "markdown",
217+
"id": "7920a719",
218+
"metadata": {},
219+
"source": [
220+
"## Summary\n",
221+
"\n",
222+
"In this section, we learned how to remove documents from MongoDB using operations such as `deleteOne()` and `deleteMany()`.\n",
223+
"\n",
224+
"We also explored how to verify deleted documents before removal and how to use `DeleteResult` to confirm how many documents were deleted."
187225
]
188226
}
189227
],

0 commit comments

Comments
 (0)