Skip to content

Commit 3731578

Browse files
Improve Sort & Limit section with clearer explanations and examples
- Added explanatory text for sort(), limit(), and skip() - Improved scenario descriptions and renamed variables for better readability - Adjusted examples to better match the workshop flow - Added documentation links with UTM tracking - Removed duplicated exercises already covered in the Find section
1 parent 52588ba commit 3731578

1 file changed

Lines changed: 63 additions & 74 deletions

File tree

java/20_sort_limit.ipynb

Lines changed: 63 additions & 74 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: Project, Sort & Limit\n",
17-
" "
16+
"# CRUD: Sort & Limit\n",
17+
"\n",
18+
"## What can `sort()` and `limit()` do?\n",
19+
"\n",
20+
"The [`sort()`](https://www.mongodb.com/docs/drivers/java/sync/current/crud/query-documents/sort/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) and [`limit()`](https://www.mongodb.com/docs/drivers/java/sync/current/crud/query-documents/limit/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) operations let us control the order and number of documents returned by a query.\n",
21+
"\n",
22+
"In this section, we will sort documents in ascending or descending order and limit how many results are returned from the collection."
1823
]
1924
},
2025
{
2126
"cell_type": "markdown",
2227
"id": "13926b8c",
2328
"metadata": {},
2429
"source": [
25-
"## Startup code"
30+
"## Startup code\n",
31+
"\n",
32+
"This cell imports the MongoDB Java Driver and helper classes, establishes the connection to MongoDB, and initializes the `library` database and `books` collection used throughout the CRUD examples."
2633
]
2734
},
2835
{
@@ -48,8 +55,14 @@
4855
"import com.mongodb.client.model.Sorts;\n",
4956
"import com.mongodb.client.model.Projections;\n",
5057
"\n",
58+
"import static com.mongodb.client.model.Projections.fields;\n",
59+
"import static com.mongodb.client.model.Projections.excludeId;\n",
60+
"import static com.mongodb.client.model.Projections.include;\n",
61+
"\n",
62+
"import static com.mongodb.client.model.Sorts.ascending;\n",
5163
"import static com.mongodb.client.model.Sorts.descending;\n",
52-
"import static com.mongodb.client.model.Filters.eq;\n",
64+
"\n",
65+
"import static com.mongodb.client.model.Filters.lte;\n",
5366
"\n",
5467
"import org.bson.Document;\n",
5568
"import org.bson.conversions.Bson;\n",
@@ -58,6 +71,10 @@
5871
"import java.util.ArrayList;\n",
5972
"import java.util.List;\n",
6073
"\n",
74+
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
75+
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
76+
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
77+
"\n",
6178
"// Set your connection String\n",
6279
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
6380
"\n",
@@ -79,7 +96,9 @@
7996
"id": "automatic-fisher",
8097
"metadata": {},
8198
"source": [
82-
"## Sort books by descending number of pages\n"
99+
"## Sort books by descending number of pages\n",
100+
"\n",
101+
"In this example, we use [`lte()`](https://www.mongodb.com/docs/manual/reference/operator/query/lte/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) to filter books published before or in `1930`, project only the `title` and `pages` fields, and use [`sort()`](https://www.mongodb.com/docs/drivers/java/sync/current/crud/query-documents/sort/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) to order the results by the number of pages in descending order."
83102
]
84103
},
85104
{
@@ -93,24 +112,26 @@
93112
},
94113
"outputs": [],
95114
"source": [
96-
"Bson projection = Projections.include(\"title\", \"pages\");\n",
97-
"\n",
98115
"FindIterable<Document> cursor = books \n",
99-
" .find() // empty filter: all books \n",
100-
" .projection(projection) // projection \n",
101-
" .sort(Sorts.descending(\"pages\")); // sort by pages descending \n",
116+
" .find(lte(\"year\", 1930)) \n",
117+
" .projection(\n",
118+
" fields(excludeId(), include(\"title\", \"pages\"))\n",
119+
" )\n",
120+
" .sort(descending(\"pages\")); \n",
102121
"\n",
103122
"for (Document book : cursor) { \n",
104123
" System.out.println(book.toJson()); \n",
105-
"} "
124+
"}"
106125
]
107126
},
108127
{
109128
"cell_type": "markdown",
110129
"id": "handled-symbol",
111130
"metadata": {},
112131
"source": [
113-
"## Limit"
132+
"## Retrieve the top 5 longest books\n",
133+
"\n",
134+
"In this example, we retrieve all books, project only the `title` and `pages` fields, sort the results by the number of pages in descending order, and use [`limit()`](https://www.mongodb.com/docs/drivers/java/sync/current/crud/query-documents/limit/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) to return only the first 5 documents."
114135
]
115136
},
116137
{
@@ -124,13 +145,11 @@
124145
},
125146
"outputs": [],
126147
"source": [
127-
"Bson projection = Projections.include(\"title\", \"pages\"); // projection \n",
128-
"\n",
129148
"FindIterable<Document> cursor = books \n",
130-
" .find() // empty filter: all books \n",
131-
" .projection(projection) // projection \n",
132-
" .sort(Sorts.descending(\"pages\")) // sort by pages descending \n",
133-
" .limit(5); // limit to 5 documents\n",
149+
" .find() \n",
150+
" .projection(include(\"title\", \"pages\")) \n",
151+
" .sort(descending(\"pages\")) \n",
152+
" .limit(5); \n",
134153
"\n",
135154
"for (Document book : cursor) { \n",
136155
" System.out.println(book.toJson()); \n",
@@ -142,7 +161,9 @@
142161
"id": "9f6caab3",
143162
"metadata": {},
144163
"source": [
145-
"## Chaining methods with find"
164+
"## Skip the first 180 books and retrieve the next 5\n",
165+
"\n",
166+
"In this example, we project only the `title` and `pages` fields, sort the books by the number of pages in ascending order, use [`skip()`](https://www.mongodb.com/docs/drivers/java/sync/current/crud/query-documents/skip/?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello) to ignore the first 180 matching documents, and return the next 5 results."
146167
]
147168
},
148169
{
@@ -156,18 +177,18 @@
156177
},
157178
"outputs": [],
158179
"source": [
159-
"Bson projection = Projections.include(\"title\", \"pages\"); // projection \n",
180+
"Bson projection = Projections.include(\"title\", \"pages\");\n",
160181
"\n",
161-
"FindIterable<Document> cursor = books \n",
162-
" .find() // empty filter: all books \n",
163-
" .projection(projection) // projection \n",
164-
" .sort(Sorts.descending(\"pages\")) // sort by pages descending \n",
165-
" .skip(4) // skip first 4 documents\n",
166-
" .limit(5); // limit to 5 documents\n",
182+
"FindIterable<Document> cursor = books\n",
183+
" .find()\n",
184+
" .projection(projection)\n",
185+
" .sort(ascending(\"pages\"))\n",
186+
" .skip(180)\n",
187+
" .limit(5);\n",
167188
"\n",
168-
"for (Document book : cursor) { \n",
169-
" System.out.println(book.toJson()); \n",
170-
"} "
189+
"for (Document book : cursor) {\n",
190+
" System.out.println(book.toJson());\n",
191+
"}"
171192
]
172193
},
173194
{
@@ -180,74 +201,42 @@
180201
},
181202
{
182203
"cell_type": "markdown",
183-
"id": "00153c48",
204+
"id": "1714facc",
184205
"metadata": {},
185206
"source": [
186-
"### Retrieve only the title field for all books.\n",
207+
"### Find the first 10 books alphabetically by title\n",
187208
"\n",
188-
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/SELECT#-1-retrieve-only-the-title-field-for-all-books)"
189-
]
190-
},
191-
{
192-
"cell_type": "code",
193-
"execution_count": null,
194-
"id": "4a4f8e26",
195-
"metadata": {
196-
"vscode": {
197-
"languageId": "java"
198-
}
199-
},
200-
"outputs": [],
201-
"source": [
202-
"// type your code here"
203-
]
204-
},
205-
{
206-
"cell_type": "markdown",
207-
"id": "ffea41f8",
208-
"metadata": {},
209-
"source": [
210-
"### Retrieve all fields except _id and authors for books in the \"History\" genre.\n",
209+
"_Hint: Use `sort()` with ascending order on the `title` field and combine it with `limit()` to return only 10 documents._\n",
211210
"\n",
212-
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/SELECT#-2-retrieve-all-fields-except-_id-and-authors-for-books-in-the-history-genre)"
211+
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/ORDER-LIMIT#-1-find-the-first-10-books-alphabetically-by-title?utm_campaign=devrel&utm_source=third-part-content&utm_medium=cta&utm_content=crud-operations-java-workshop&utm_term=ricardo.mello)"
213212
]
214213
},
215214
{
216215
"cell_type": "code",
217216
"execution_count": null,
218-
"id": "f02579f7",
217+
"id": "5409c76d",
219218
"metadata": {
220219
"vscode": {
221220
"languageId": "java"
222221
}
223222
},
224223
"outputs": [],
225224
"source": [
226-
"// type your code here\n"
225+
"//TYPE YOUR CODE HERE\n",
226+
"books.find()\n",
227+
" .sort(<REPLACE_WITH_SORT>)\n",
228+
" .limit(<REPLACE_WITH_LIMIT>)\n",
229+
" .forEach(book -> System.out.println(\"Book: \" + book.toJson()));"
227230
]
228231
},
229232
{
230233
"cell_type": "markdown",
231-
"id": "1714facc",
234+
"id": "bc2d6bee",
232235
"metadata": {},
233236
"source": [
234-
"### Find the first 10 books alphabetically by title.\n",
237+
"## Summary\n",
235238
"\n",
236-
"[Solution here](https://mongodb-developer.github.io/sql-to-query-api-lab/docs/CRUD/ORDER-LIMIT#-1-find-the-first-10-books-alphabetically-by-title)"
237-
]
238-
},
239-
{
240-
"cell_type": "code",
241-
"execution_count": null,
242-
"id": "5409c76d",
243-
"metadata": {
244-
"vscode": {
245-
"languageId": "java"
246-
}
247-
},
248-
"outputs": [],
249-
"source": [
250-
"// type your code here\n"
239+
"In this section, we learned how to use `sort()`, `skip()`, and `limit()` to control the order and number of documents returned by a query. These operations are commonly combined with `find()` to build more efficient and readable queries."
251240
]
252241
}
253242
],

0 commit comments

Comments
 (0)