|
37 | 37 | "outputs": [], |
38 | 38 | "source": [ |
39 | 39 | "// Import the MongoDB Driver using Maven\n", |
40 | | - "%maven org.mongodb:mongodb-driver-sync:5.0.0\n", |
41 | | - " \n", |
| 40 | + "%maven org.mongodb:mongodb-driver-sync:5.5.1\n", |
| 41 | + "%maven org.slf4j:slf4j-simple:1.7.36\n", |
| 42 | + "\n", |
42 | 43 | "import com.mongodb.client.MongoClient;\n", |
43 | 44 | "import com.mongodb.client.MongoClients;\n", |
44 | 45 | "import com.mongodb.client.MongoDatabase;\n", |
45 | 46 | "import com.mongodb.client.MongoCollection;\n", |
46 | 47 | "import com.mongodb.client.AggregateIterable;\n", |
47 | | - "\n", |
| 48 | + "import com.mongodb.client.model.Aggregates;\n", |
48 | 49 | "import com.mongodb.client.model.Projections;\n", |
| 50 | + "\n", |
49 | 51 | "import static com.mongodb.client.model.Filters.gt;\n", |
| 52 | + "import static com.mongodb.client.model.Filters.eq;\n", |
| 53 | + "import static com.mongodb.client.model.Projections.include;\n", |
| 54 | + "import static com.mongodb.client.model.Projections.excludeId;\n", |
| 55 | + "import static com.mongodb.client.model.Projections.fields;\n", |
50 | 56 | "\n", |
51 | | - "import com.mongodb.client.model.Aggregates;\n", |
| 57 | + "import static com.mongodb.client.model.Aggregates.project;\n", |
52 | 58 | "import static com.mongodb.client.model.Aggregates.match;\n", |
53 | 59 | "import static com.mongodb.client.model.Aggregates.limit;\n", |
54 | 60 | "import static com.mongodb.client.model.Aggregates.sort;\n", |
55 | | - "import static com.mongodb.client.model.Sorts.ascending;\n", |
| 61 | + "import static com.mongodb.client.model.Sorts.descending;\n", |
56 | 62 | "\n", |
57 | 63 | "import org.bson.Document;\n", |
58 | 64 | "import org.bson.conversions.Bson;\n", |
59 | 65 | "\n", |
60 | 66 | "import org.bson.Document;\n", |
61 | 67 | "import org.bson.conversions.Bson;\n", |
62 | 68 | "\n", |
| 69 | + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", |
| 70 | + "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", |
| 71 | + "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", |
| 72 | + "\n", |
63 | 73 | "// Set your connection String\n", |
64 | 74 | "String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n", |
65 | 75 | "\n", |
|
83 | 93 | "source": [ |
84 | 94 | "## $match\n", |
85 | 95 | "\n", |
86 | | - "We'll get all books written after 2010." |
| 96 | + "We’ll use $match to return only the book with the title \"Treasure of the Sun\"." |
87 | 97 | ] |
88 | 98 | }, |
89 | 99 | { |
|
97 | 107 | }, |
98 | 108 | "outputs": [], |
99 | 109 | "source": [ |
100 | | - "Bson yearFilter = gt(\"year\", 2010);\n", |
101 | | - "Bson matchStage = match(yearFilter);\n", |
102 | | - "\n", |
| 110 | + "Bson titleFilter = eq(\"title\", \"Treasure of the Sun\");\n", |
| 111 | + "Bson matchStage = match(titleFilter);\n", |
103 | 112 | "List<Bson> aggregationPipeline = List.of(matchStage);\n", |
104 | 113 | "\n", |
105 | 114 | "AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n", |
|
110 | 119 | "}" |
111 | 120 | ] |
112 | 121 | }, |
| 122 | + { |
| 123 | + "cell_type": "markdown", |
| 124 | + "id": "74f5fd5a", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "## $project\n", |
| 128 | + "\n", |
| 129 | + "We’ll project only the `title` and `year` fields for the book \"Treasure of the Sun\", excluding the `_id` field from the result." |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": null, |
| 135 | + "id": "50b8aa38", |
| 136 | + "metadata": { |
| 137 | + "vscode": { |
| 138 | + "languageId": "java" |
| 139 | + } |
| 140 | + }, |
| 141 | + "outputs": [], |
| 142 | + "source": [ |
| 143 | + "Bson titleFilter = eq(\"title\", \"Treasure of the Sun\");\n", |
| 144 | + "Bson matchStage = match(titleFilter);\n", |
| 145 | + "\n", |
| 146 | + "Bson projectStage = project(\n", |
| 147 | + " fields(\n", |
| 148 | + " include(\"title\", \"year\"),\n", |
| 149 | + " excludeId()\n", |
| 150 | + " )\n", |
| 151 | + ");\n", |
| 152 | + "\n", |
| 153 | + "List<Bson> aggregationPipeline = List.of(matchStage, projectStage);\n", |
| 154 | + "\n", |
| 155 | + "AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n", |
| 156 | + "\n", |
| 157 | + "// Iterate through the results\n", |
| 158 | + "for (Document doc : result) {\n", |
| 159 | + " System.out.println(\"book: \" + doc.toJson());\n", |
| 160 | + "}\n" |
| 161 | + ] |
| 162 | + }, |
113 | 163 | { |
114 | 164 | "cell_type": "markdown", |
115 | 165 | "id": "9f6caab3", |
116 | 166 | "metadata": {}, |
117 | 167 | "source": [ |
118 | | - "### $limit\n", |
| 168 | + "## $limit\n", |
119 | 169 | "\n", |
120 | | - "We'll get books published after 2010 and limit the results to 10 documents." |
| 170 | + "We’ll first match books published after 2010, then use `$limit` to return only 3 documents." |
121 | 171 | ] |
122 | 172 | }, |
123 | 173 | { |
|
133 | 183 | "source": [ |
134 | 184 | "Bson yearFilter = gt(\"year\", 2010);\n", |
135 | 185 | "Bson matchStage = match(yearFilter);\n", |
136 | | - "Bson limitStage = limit(10);\n", |
| 186 | + "Bson limitStage = limit(3);\n", |
137 | 187 | "\n", |
138 | 188 | "List<Bson> aggregationPipeline = List.of(matchStage, limitStage);\n", |
139 | 189 | "\n", |
140 | 190 | "AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n", |
141 | 191 | "\n", |
| 192 | + "// Iterate through the results\n", |
142 | 193 | "for (Document doc : result) {\n", |
143 | 194 | " System.out.println(\"book: \" + doc.toJson());\n", |
144 | 195 | "}" |
|
155 | 206 | "id": "worth-windows", |
156 | 207 | "metadata": {}, |
157 | 208 | "source": [ |
158 | | - "### $sort\n", |
| 209 | + "## $sort\n", |
159 | 210 | "\n", |
160 | | - "We'll sort the results by publication year in ascending order." |
| 211 | + "We’ll sort the books by the number of pages in descending order, limit the result to 3 documents, and project only the title and pages fields." |
161 | 212 | ] |
162 | 213 | }, |
163 | 214 | { |
|
171 | 222 | }, |
172 | 223 | "outputs": [], |
173 | 224 | "source": [ |
174 | | - "Bson yearFilter = gt(\"year\", 2010);\n", |
175 | | - "Bson matchStage = match(yearFilter);\n", |
176 | | - "Bson limitStage = limit(10);\n", |
177 | | - "Bson sortStage = sort(ascending(\"year\"));\n", |
| 225 | + "Bson projectStage = project(\n", |
| 226 | + " fields(\n", |
| 227 | + " include(\"title\", \"pages\"),\n", |
| 228 | + " excludeId()\n", |
| 229 | + " )\n", |
| 230 | + ");\n", |
| 231 | + "\n", |
| 232 | + "Bson sortStage = sort(descending(\"pages\"));\n", |
| 233 | + "Bson limitStage = limit(3);\n", |
178 | 234 | "\n", |
179 | 235 | "List<Bson> aggregationPipeline = List.of(\n", |
180 | | - " matchStage,\n", |
181 | | - " limitStage,\n", |
182 | | - " sortStage\n", |
| 236 | + " projectStage,\n", |
| 237 | + " sortStage,\n", |
| 238 | + " limitStage\n", |
183 | 239 | ");\n", |
184 | 240 | "\n", |
185 | 241 | "AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n", |
186 | 242 | "\n", |
| 243 | + "// Iterate through the results\n", |
187 | 244 | "for (Document doc : result) {\n", |
188 | 245 | " System.out.println(\"book: \" + doc.toJson());\n", |
189 | 246 | "}" |
|
218 | 275 | }, |
219 | 276 | "outputs": [], |
220 | 277 | "source": [ |
221 | | - "// type your code here" |
| 278 | + "// TYPE YOUR CODE HERE\n", |
| 279 | + "\n", |
| 280 | + "AggregateIterable<Document> result = books.aggregate(\n", |
| 281 | + " List.of(<REPLACE_WITH_MATCH_STAGE>) \n", |
| 282 | + ");\n", |
| 283 | + "\n", |
| 284 | + "for (Document doc : result) {\n", |
| 285 | + " System.out.println(\"book: \" + doc.toJson());\n", |
| 286 | + "}" |
222 | 287 | ] |
223 | 288 | }, |
224 | 289 | { |
|
242 | 307 | }, |
243 | 308 | "outputs": [], |
244 | 309 | "source": [ |
245 | | - "// type your code here" |
| 310 | + "// TYPE YOUR CODE HERE\n", |
| 311 | + "\n", |
| 312 | + "AggregateIterable<Document> result = books.aggregate(\n", |
| 313 | + " List.of(\n", |
| 314 | + " <REPLACE_WITH_MATCH_STAGE>,\n", |
| 315 | + " <REPLACE_WITH_PROJECT_STAGE>\n", |
| 316 | + " )\n", |
| 317 | + ");\n", |
| 318 | + "\n", |
| 319 | + "for (Document doc : result) {\n", |
| 320 | + " System.out.println(\"book: \" + doc.toJson());\n", |
| 321 | + "}" |
246 | 322 | ] |
247 | 323 | } |
248 | 324 | ], |
|
0 commit comments