Skip to content

Commit 770dae1

Browse files
Improve Insert CRUD section and exercises
- Added introductory explanations for insert operations - Added examples using insertOne() and insertMany() - Improved challenge descriptions and hints - Added examples of review documents for guidance - Added MongoDB documentation links with UTM tracking - Refined wording and examples for better readability
1 parent c7d46a8 commit 770dae1

1 file changed

Lines changed: 55 additions & 8 deletions

File tree

java/30_insert.ipynb

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@
1414
"metadata": {},
1515
"source": [
1616
"# 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."
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 insert examples."
2633
]
2734
},
2835
{
@@ -52,6 +59,10 @@
5259
"\n",
5360
"import java.util.List;\n",
5461
"\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",
5566
"// Set your connection String\n",
5667
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
5768
"\n",
@@ -81,7 +92,9 @@
8192
"id": "handled-symbol",
8293
"metadata": {},
8394
"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."
8598
]
8699
},
87100
{
@@ -106,7 +119,9 @@
106119
"id": "worth-windows",
107120
"metadata": {},
108121
"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."
110125
]
111126
},
112127
{
@@ -131,7 +146,11 @@
131146
"id": "9f6caab3",
132147
"metadata": {},
133148
"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`."
135154
]
136155
},
137156
{
@@ -166,7 +185,20 @@
166185
"source": [
167186
"### Insert 4 more reviews for bookId \"0786222727\".\n",
168187
"\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)"
170202
]
171203
},
172204
{
@@ -180,8 +212,23 @@
180212
},
181213
"outputs": [],
182214
"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."
185232
]
186233
}
187234
],

0 commit comments

Comments
 (0)