|
14 | 14 | "metadata": {}, |
15 | 15 | "source": [ |
16 | 16 | "# Basic CRUD: Insert\n", |
17 | | - " " |
| 17 | + "\n", |
| 18 | + "## What can the `insert` operation do?\n", |
| 19 | + "\n", |
| 20 | + "The [`insert`](https://www.mongodb.com/docs/drivers/java/sync/current/crud/insert/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) operations let us add new documents to a MongoDB collection.\n", |
| 21 | + "\n", |
| 22 | + "In this section, we will use operations such as `insertOne()` and `insertMany()` to create and store documents in 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 insert examples." |
26 | 33 | ] |
27 | 34 | }, |
28 | 35 | { |
|
52 | 59 | "\n", |
53 | 60 | "import java.util.List;\n", |
54 | 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", |
55 | 66 | "// Set your connection String\n", |
56 | 67 | "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", |
57 | 68 | "\n", |
|
81 | 92 | "id": "handled-symbol", |
82 | 93 | "metadata": {}, |
83 | 94 | "source": [ |
84 | | - "### Insert one book" |
| 95 | + "### Insert one book\n", |
| 96 | + "\n", |
| 97 | + "In this example, we create a new `Document` representing a book and use `insertOne()` to add it to the `books` collection." |
85 | 98 | ] |
86 | 99 | }, |
87 | 100 | { |
|
106 | 119 | "id": "worth-windows", |
107 | 120 | "metadata": {}, |
108 | 121 | "source": [ |
109 | | - "### Read the book we just inserted, using the year" |
| 122 | + "### Find the inserted document\n", |
| 123 | + "\n", |
| 124 | + "In this example, we query the `books` collection to verify that the document inserted with the year `1500` was successfully stored in MongoDB." |
110 | 125 | ] |
111 | 126 | }, |
112 | 127 | { |
|
131 | 146 | "id": "9f6caab3", |
132 | 147 | "metadata": {}, |
133 | 148 | "source": [ |
134 | | - "### Can you find the same document, but using the `_id` instead? Fix the code below!" |
| 149 | + "### Find the same document using the `_id`\n", |
| 150 | + "\n", |
| 151 | + "Can you find the same document again, but this time using the `_id` field instead of the `year`?\n", |
| 152 | + "\n", |
| 153 | + "Fix the code below by adding the correct `ObjectId`." |
135 | 154 | ] |
136 | 155 | }, |
137 | 156 | { |
|
166 | 185 | "source": [ |
167 | 186 | "### Insert 4 more reviews for bookId \"0786222727\".\n", |
168 | 187 | "\n", |
169 | | - "[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/INSERT-DELETE#-1-insert-4-more-reviews-for-bookid-0786222727)" |
| 188 | + "_Hint: Use `insertMany()` with a `List.of()` containing multiple `Document` objects._\n", |
| 189 | + "\n", |
| 190 | + "Example review document:\n", |
| 191 | + "\n", |
| 192 | + "```json\n", |
| 193 | + "{\n", |
| 194 | + " \"text\": \"Amazing book!\",\n", |
| 195 | + " \"rating\": 5,\n", |
| 196 | + " \"name\": \"Ricardo\",\n", |
| 197 | + " \"bookId\": \"0786222727\"\n", |
| 198 | + "}\n", |
| 199 | + "````\n", |
| 200 | + "\n", |
| 201 | + "[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/INSERT-DELETE#-1-insert-4-more-reviews-for-bookid-0786222727?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello)" |
170 | 202 | ] |
171 | 203 | }, |
172 | 204 | { |
|
180 | 212 | }, |
181 | 213 | "outputs": [], |
182 | 214 | "source": [ |
183 | | - "// type your code here\n", |
184 | | - " " |
| 215 | + "// TYPE YOUR CODE HERE\n", |
| 216 | + "\n", |
| 217 | + "var reviews = library.getCollection(\"reviews\");\n", |
| 218 | + "\n", |
| 219 | + "<REPLACE_WITH_INSERT_OPERATION>;" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "markdown", |
| 224 | + "id": "7e9330dd", |
| 225 | + "metadata": {}, |
| 226 | + "source": [ |
| 227 | + "## Summary\n", |
| 228 | + "\n", |
| 229 | + "In this section, we learned how to insert new documents into MongoDB using operations such as `insertOne()` and `insertMany()`.\n", |
| 230 | + "\n", |
| 231 | + "We also explored how MongoDB automatically generates `_id` values and how inserted documents can be queried afterward." |
185 | 232 | ] |
186 | 233 | } |
187 | 234 | ], |
|
0 commit comments