|
13 | 13 | "id": "3b936925-e295-489a-b508-2b99c0160217", |
14 | 14 | "metadata": {}, |
15 | 15 | "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." |
18 | 23 | ] |
19 | 24 | }, |
20 | 25 | { |
|
54 | 59 | "\n", |
55 | 60 | "import java.util.List;\n", |
56 | 61 | "\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", |
57 | 66 | "// Set your connection String\n", |
58 | 67 | "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", |
59 | 68 | "\n", |
|
77 | 86 | "source": [ |
78 | 87 | "## Insert one book\n", |
79 | 88 | "\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." |
81 | 90 | ] |
82 | 91 | }, |
83 | 92 | { |
|
91 | 100 | }, |
92 | 101 | "outputs": [], |
93 | 102 | "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", |
100 | 107 | "\n", |
101 | 108 | "books.insertOne(platero);" |
102 | 109 | ] |
|
106 | 113 | "id": "9f6caab3", |
107 | 114 | "metadata": {}, |
108 | 115 | "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." |
110 | 119 | ] |
111 | 120 | }, |
112 | 121 | { |
|
131 | 140 | "id": "worth-windows", |
132 | 141 | "metadata": {}, |
133 | 142 | "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." |
135 | 148 | ] |
136 | 149 | }, |
137 | 150 | { |
|
145 | 158 | }, |
146 | 159 | "outputs": [], |
147 | 160 | "source": [ |
148 | | - "Document bookFrom1500 = new Document(\"year\", 1500);\n", |
| 161 | + "Document plateroFilter = new Document(\"_id\", \"platero\");\n", |
149 | 162 | "\n", |
150 | | - "DeleteResult deleteResult = books.deleteOne(new Document(\"_id\", \"platero\"));\n", |
| 163 | + "DeleteResult deleteResult = books.deleteOne(plateroFilter);\n", |
151 | 164 | "\n", |
152 | 165 | "// Check the number of deleted documents\n", |
153 | 166 | "long deletedCount = deleteResult.getDeletedCount();\n", |
| 167 | + "\n", |
154 | 168 | "System.out.println(\"Deleted \" + deletedCount + \" document(s).\");" |
155 | 169 | ] |
156 | 170 | }, |
|
169 | 183 | "source": [ |
170 | 184 | "### Delete all the reviews for bookId \"0786222727\" through a single command.\n", |
171 | 185 | "\n", |
| 186 | + "\n", |
| 187 | + "_Hint: You may need an operation capable of deleting multiple documents at once._\n", |
| 188 | + "\n", |
| 189 | + "\n", |
172 | 190 | "[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)" |
173 | 191 | ] |
174 | 192 | }, |
|
183 | 201 | }, |
184 | 202 | "outputs": [], |
185 | 203 | "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." |
187 | 225 | ] |
188 | 226 | } |
189 | 227 | ], |
|
0 commit comments