|
13 | 13 | "id": "3b936925-e295-489a-b508-2b99c0160217", |
14 | 14 | "metadata": {}, |
15 | 15 | "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." |
18 | 23 | ] |
19 | 24 | }, |
20 | 25 | { |
21 | 26 | "cell_type": "markdown", |
22 | 27 | "id": "13926b8c", |
23 | 28 | "metadata": {}, |
24 | 29 | "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." |
26 | 33 | ] |
27 | 34 | }, |
28 | 35 | { |
|
48 | 55 | "import com.mongodb.client.model.Sorts;\n", |
49 | 56 | "import com.mongodb.client.model.Projections;\n", |
50 | 57 | "\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", |
51 | 63 | "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", |
53 | 66 | "\n", |
54 | 67 | "import org.bson.Document;\n", |
55 | 68 | "import org.bson.conversions.Bson;\n", |
|
58 | 71 | "import java.util.ArrayList;\n", |
59 | 72 | "import java.util.List;\n", |
60 | 73 | "\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", |
61 | 78 | "// Set your connection String\n", |
62 | 79 | "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", |
63 | 80 | "\n", |
|
79 | 96 | "id": "automatic-fisher", |
80 | 97 | "metadata": {}, |
81 | 98 | "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." |
83 | 102 | ] |
84 | 103 | }, |
85 | 104 | { |
|
93 | 112 | }, |
94 | 113 | "outputs": [], |
95 | 114 | "source": [ |
96 | | - "Bson projection = Projections.include(\"title\", \"pages\");\n", |
97 | | - "\n", |
98 | 115 | "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", |
102 | 121 | "\n", |
103 | 122 | "for (Document book : cursor) { \n", |
104 | 123 | " System.out.println(book.toJson()); \n", |
105 | | - "} " |
| 124 | + "}" |
106 | 125 | ] |
107 | 126 | }, |
108 | 127 | { |
109 | 128 | "cell_type": "markdown", |
110 | 129 | "id": "handled-symbol", |
111 | 130 | "metadata": {}, |
112 | 131 | "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." |
114 | 135 | ] |
115 | 136 | }, |
116 | 137 | { |
|
124 | 145 | }, |
125 | 146 | "outputs": [], |
126 | 147 | "source": [ |
127 | | - "Bson projection = Projections.include(\"title\", \"pages\"); // projection \n", |
128 | | - "\n", |
129 | 148 | "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", |
134 | 153 | "\n", |
135 | 154 | "for (Document book : cursor) { \n", |
136 | 155 | " System.out.println(book.toJson()); \n", |
|
142 | 161 | "id": "9f6caab3", |
143 | 162 | "metadata": {}, |
144 | 163 | "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." |
146 | 167 | ] |
147 | 168 | }, |
148 | 169 | { |
|
156 | 177 | }, |
157 | 178 | "outputs": [], |
158 | 179 | "source": [ |
159 | | - "Bson projection = Projections.include(\"title\", \"pages\"); // projection \n", |
| 180 | + "Bson projection = Projections.include(\"title\", \"pages\");\n", |
160 | 181 | "\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", |
167 | 188 | "\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 | + "}" |
171 | 192 | ] |
172 | 193 | }, |
173 | 194 | { |
|
180 | 201 | }, |
181 | 202 | { |
182 | 203 | "cell_type": "markdown", |
183 | | - "id": "00153c48", |
| 204 | + "id": "1714facc", |
184 | 205 | "metadata": {}, |
185 | 206 | "source": [ |
186 | | - "### Retrieve only the title field for all books.\n", |
| 207 | + "### Find the first 10 books alphabetically by title\n", |
187 | 208 | "\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", |
211 | 210 | "\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)" |
213 | 212 | ] |
214 | 213 | }, |
215 | 214 | { |
216 | 215 | "cell_type": "code", |
217 | 216 | "execution_count": null, |
218 | | - "id": "f02579f7", |
| 217 | + "id": "5409c76d", |
219 | 218 | "metadata": { |
220 | 219 | "vscode": { |
221 | 220 | "languageId": "java" |
222 | 221 | } |
223 | 222 | }, |
224 | 223 | "outputs": [], |
225 | 224 | "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()));" |
227 | 230 | ] |
228 | 231 | }, |
229 | 232 | { |
230 | 233 | "cell_type": "markdown", |
231 | | - "id": "1714facc", |
| 234 | + "id": "bc2d6bee", |
232 | 235 | "metadata": {}, |
233 | 236 | "source": [ |
234 | | - "### Find the first 10 books alphabetically by title.\n", |
| 237 | + "## Summary\n", |
235 | 238 | "\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." |
251 | 240 | ] |
252 | 241 | } |
253 | 242 | ], |
|
0 commit comments