|
13 | 13 | "id": "3b936925-e295-489a-b508-2b99c0160217", |
14 | 14 | "metadata": {}, |
15 | 15 | "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." |
18 | 23 | ] |
19 | 24 | }, |
20 | 25 | { |
21 | 26 | "cell_type": "markdown", |
22 | 27 | "id": "dependent-boundary", |
23 | 28 | "metadata": {}, |
24 | 29 | "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." |
26 | 33 | ] |
27 | 34 | }, |
28 | 35 | { |
|
57 | 64 | "import org.bson.Document;\n", |
58 | 65 | "import org.bson.conversions.Bson;\n", |
59 | 66 | "\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", |
60 | 71 | "// Set your connection String\n", |
61 | 72 | "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", |
62 | 73 | "\n", |
|
78 | 89 | "id": "handled-symbol", |
79 | 90 | "metadata": {}, |
80 | 91 | "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()`." |
82 | 96 | ] |
83 | 97 | }, |
84 | 98 | { |
|
92 | 106 | }, |
93 | 107 | "outputs": [], |
94 | 108 | "source": [ |
95 | | - "Document elQuijote = new Document();\n", |
96 | | - "elQuijote\n", |
| 109 | + "Document elQuijote = new Document()\n", |
97 | 110 | " .append(\"_id\", \"quijote\")\n", |
98 | 111 | " .append(\"title\", \"El Quijote\")\n", |
99 | 112 | " .append(\"year\", 1501);\n", |
|
106 | 119 | "id": "9f6caab3", |
107 | 120 | "metadata": {}, |
108 | 121 | "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." |
110 | 125 | ] |
111 | 126 | }, |
112 | 127 | { |
|
135 | 150 | } |
136 | 151 | }, |
137 | 152 | "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" |
147 | 161 | ] |
148 | 162 | }, |
149 | 163 | { |
|
186 | 200 | "id": "737b00e6", |
187 | 201 | "metadata": {}, |
188 | 202 | "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." |
190 | 206 | ] |
191 | 207 | }, |
192 | 208 | { |
|
205 | 221 | "System.out.println(\"Book: \" + aBook.toJson());" |
206 | 222 | ] |
207 | 223 | }, |
208 | | - { |
209 | | - "cell_type": "markdown", |
210 | | - "id": "398d39bb", |
211 | | - "metadata": {}, |
212 | | - "source": [ |
213 | | - "### Upsert" |
214 | | - ] |
215 | | - }, |
216 | 224 | { |
217 | 225 | "cell_type": "markdown", |
218 | 226 | "id": "ba85cf52", |
219 | 227 | "metadata": {}, |
220 | 228 | "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." |
222 | 237 | ] |
223 | 238 | }, |
224 | 239 | { |
|
244 | 259 | "id": "e5ddfd06", |
245 | 260 | "metadata": {}, |
246 | 261 | "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." |
248 | 265 | ] |
249 | 266 | }, |
250 | 267 | { |
|
282 | 299 | "}" |
283 | 300 | ] |
284 | 301 | }, |
| 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 | + }, |
285 | 332 | { |
286 | 333 | "cell_type": "markdown", |
287 | 334 | "id": "b8bfa616", |
|
295 | 342 | "id": "fe70e1e8", |
296 | 343 | "metadata": {}, |
297 | 344 | "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", |
299 | 346 | "\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)" |
301 | 350 | ] |
302 | 351 | }, |
303 | 352 | { |
|
311 | 360 | }, |
312 | 361 | "outputs": [], |
313 | 362 | "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." |
315 | 383 | ] |
316 | 384 | } |
317 | 385 | ], |
|
0 commit comments