Skip to content

Commit bc8a889

Browse files
Improve Update CRUD section and examples
- Added introductory explanations for update operations - Added examples using updateOne(), set(), addToSet(), and currentTimestamp() - Added upsert examples and explanations - Improved challenge instructions and hints - Refined code formatting and variable naming for readability - Added MongoDB documentation links with UTM tracking
1 parent 4a64684 commit bc8a889

2 files changed

Lines changed: 101 additions & 31 deletions

File tree

java/40_delete.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"id": "dependent-boundary",
2828
"metadata": {},
2929
"source": [
30-
"## Startup code"
30+
"## Startup code\n",
31+
"\n",
32+
"This cell imports the MongoDB Java Driver, connects to MongoDB, and initializes the `library` database and `books` collection used in the delete examples."
3133
]
3234
},
3335
{

java/50_update.ipynb

Lines changed: 98 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@
1313
"id": "3b936925-e295-489a-b508-2b99c0160217",
1414
"metadata": {},
1515
"source": [
16-
"# CRUD: Update\n",
17-
" "
16+
"# Basic CRUD: Update\n",
17+
"\n",
18+
"## What can the `update` operation do?\n",
19+
"\n",
20+
"The [`update`](https://www.mongodb.com/docs/drivers/java/sync/current/crud/update-documents/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) operations let us modify existing documents in a MongoDB collection.\n",
21+
"\n",
22+
"In this section, we will use operations such as `updateOne()` and `updateMany()` to update fields in documents from the `books` collection."
1823
]
1924
},
2025
{
2126
"cell_type": "markdown",
2227
"id": "dependent-boundary",
2328
"metadata": {},
2429
"source": [
25-
"## Startup code"
30+
"## Startup code\n",
31+
"\n",
32+
"This cell imports the MongoDB Java Driver, connects to MongoDB, and initializes the `library` database and `books` collection used in the update examples."
2633
]
2734
},
2835
{
@@ -57,6 +64,10 @@
5764
"import org.bson.Document;\n",
5865
"import org.bson.conversions.Bson;\n",
5966
"\n",
67+
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
68+
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
69+
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
70+
"\n",
6071
"// Set your connection String\n",
6172
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
6273
"\n",
@@ -78,7 +89,10 @@
7889
"id": "handled-symbol",
7990
"metadata": {},
8091
"source": [
81-
"## Insert one book"
92+
"## Insert one book\n",
93+
"\n",
94+
"In this example, we insert a new document into the `books` collection. \n",
95+
"We will later update this same document using `updateOne()`."
8296
]
8397
},
8498
{
@@ -92,8 +106,7 @@
92106
},
93107
"outputs": [],
94108
"source": [
95-
"Document elQuijote = new Document();\n",
96-
"elQuijote\n",
109+
"Document elQuijote = new Document()\n",
97110
" .append(\"_id\", \"quijote\")\n",
98111
" .append(\"title\", \"El Quijote\")\n",
99112
" .append(\"year\", 1501);\n",
@@ -106,7 +119,9 @@
106119
"id": "9f6caab3",
107120
"metadata": {},
108121
"source": [
109-
"### Show the inserted book"
122+
"### Find inserted book\n",
123+
"\n",
124+
"Before updating the document, we query the `books` collection to confirm that the `El Quijote` document was successfully inserted."
110125
]
111126
},
112127
{
@@ -135,15 +150,14 @@
135150
}
136151
},
137152
"source": [
138-
"### Update that book"
139-
]
140-
},
141-
{
142-
"cell_type": "markdown",
143-
"id": "50ea627a",
144-
"metadata": {},
145-
"source": [
146-
"Use the 📗 [updateOne](https://www.mongodb.com/docs/drivers/java/sync/current/usage-examples/updateOne/) documentation."
153+
"### Update the inserted book\n",
154+
"\n",
155+
"In this example, we use `updateOne()` to modify the `El Quijote` document.\n",
156+
"\n",
157+
"The update operation:\n",
158+
"- adds a new field called `newField`\n",
159+
"- adds `\"Chivalry\"` to the `genres` array\n",
160+
"- updates the `lastUpdated` field with the current timestamp"
147161
]
148162
},
149163
{
@@ -186,7 +200,9 @@
186200
"id": "737b00e6",
187201
"metadata": {},
188202
"source": [
189-
"Now check the contents of the book, that has changed!"
203+
"### Verify the updated document \n",
204+
"\n",
205+
"After running the update operation, we query the `books` collection again to confirm that the document was successfully modified."
190206
]
191207
},
192208
{
@@ -205,20 +221,19 @@
205221
"System.out.println(\"Book: \" + aBook.toJson());"
206222
]
207223
},
208-
{
209-
"cell_type": "markdown",
210-
"id": "398d39bb",
211-
"metadata": {},
212-
"source": [
213-
"### Upsert"
214-
]
215-
},
216224
{
217225
"cell_type": "markdown",
218226
"id": "ba85cf52",
219227
"metadata": {},
220228
"source": [
221-
"### Delete the book, so it's not there and can't be updated"
229+
"## Understanding `upsert`\n",
230+
"\n",
231+
"[`upsert`](https://www.mongodb.com/docs/drivers/java/sync/current/crud/update-documents/upsert/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) combines **update** and **insert** behavior in a single operation.\n",
232+
"\n",
233+
"If a matching document exists, MongoDB updates it. \n",
234+
"If no document matches the filter, MongoDB inserts a new one instead.\n",
235+
"\n",
236+
"To demonstrate this behavior, we first delete the `El Quijote` document so it no longer exists in the collection."
222237
]
223238
},
224239
{
@@ -244,7 +259,9 @@
244259
"id": "e5ddfd06",
245260
"metadata": {},
246261
"source": [
247-
"### Upsert: we try to update it but will get inserted instead"
262+
"### Run the update operation with `upsert`\n",
263+
"\n",
264+
"Now that the document no longer exists in the collection, we run the update operation again with `upsert(true)` enabled."
248265
]
249266
},
250267
{
@@ -282,6 +299,36 @@
282299
"}"
283300
]
284301
},
302+
{
303+
"cell_type": "markdown",
304+
"id": "e93a39fc",
305+
"metadata": {},
306+
"source": [
307+
"### Verify the upserted document\n",
308+
"\n",
309+
"Since the document did not exist, MongoDB inserted a new one during the update operation with `upsert(true)` enabled.\n",
310+
"\n",
311+
"We can now query the collection again to confirm that the document was created."
312+
]
313+
},
314+
{
315+
"cell_type": "code",
316+
"execution_count": null,
317+
"id": "383fd2e3",
318+
"metadata": {
319+
"vscode": {
320+
"languageId": "java"
321+
}
322+
},
323+
"outputs": [],
324+
"source": [
325+
"Document upsertedBook = books.find(\n",
326+
" new Document(\"_id\", \"quijote\")\n",
327+
").first();\n",
328+
"\n",
329+
"System.out.println(\"Book: \" + upsertedBook.toJson());"
330+
]
331+
},
285332
{
286333
"cell_type": "markdown",
287334
"id": "b8bfa616",
@@ -295,9 +342,11 @@
295342
"id": "fe70e1e8",
296343
"metadata": {},
297344
"source": [
298-
"### Update the pages of the book \"Treasure of the Sun\" to 449.\n",
345+
"### Update the pages of the book `\"Treasure of the Sun\"` to `449`\n",
299346
"\n",
300-
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/UPDATE#-1-update-the-pages-of-the-book-treasure-of-the-sun-to-449)"
347+
"_Hint: Use `updateOne()` together with `Updates.set()` to modify the value of a single field._\n",
348+
"\n",
349+
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/UPDATE#-1-update-the-pages-of-the-book-treasure-of-the-sun-to-449?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello)"
301350
]
302351
},
303352
{
@@ -311,7 +360,26 @@
311360
},
312361
"outputs": [],
313362
"source": [
314-
"// type your code here\n"
363+
"//TYPE YOUR CODE HERE\n",
364+
"\n",
365+
"var query = <REPLACE_WITH_QUERY>;\n",
366+
"var update = <REPLACE_WITH_UPDATE>;\n",
367+
"\n",
368+
"UpdateResult updateResult = <REPLACE_WITH_UPDATE_OPERATION>;\n",
369+
"\n",
370+
"System.out.println(\"Modified document count: \" + updateResult.getModifiedCount());"
371+
]
372+
},
373+
{
374+
"cell_type": "markdown",
375+
"id": "6865fa4c",
376+
"metadata": {},
377+
"source": [
378+
"## Summary\n",
379+
"\n",
380+
"In this section, we learned how to modify existing documents in MongoDB using operations such as `updateOne()` and update operators like `set()` and `addToSet()`.\n",
381+
"\n",
382+
"We also explored `upsert` behavior, where MongoDB inserts a new document if no existing document matches the update filter."
315383
]
316384
}
317385
],

0 commit comments

Comments
 (0)