Skip to content

Commit 77534f8

Browse files
Included challenge description and removed unnecessary code.
1 parent e03697d commit 77534f8

5 files changed

Lines changed: 66 additions & 52 deletions

java/100_aggregation_pipeline_match.ipynb

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@
4343
"import com.mongodb.client.MongoClients;\n",
4444
"import com.mongodb.client.MongoDatabase;\n",
4545
"import com.mongodb.client.MongoCollection;\n",
46-
"import com.mongodb.client.FindIterable;\n",
4746
"import com.mongodb.client.AggregateIterable;\n",
4847
"\n",
4948
"import com.mongodb.client.model.Projections;\n",
50-
"import static com.mongodb.client.model.Filters.gte;\n",
51-
"import static com.mongodb.client.model.Filters.and;\n",
5249
"import static com.mongodb.client.model.Filters.gt;\n",
5350
"\n",
5451
"import com.mongodb.client.model.Aggregates;\n",
5552
"import static com.mongodb.client.model.Aggregates.match;\n",
5653
"import static com.mongodb.client.model.Aggregates.limit;\n",
5754
"import static com.mongodb.client.model.Aggregates.sort;\n",
55+
"import static com.mongodb.client.model.Sorts.ascending;\n",
56+
"\n",
57+
"import org.bson.Document;\n",
58+
"import org.bson.conversions.Bson;\n",
5859
"\n",
5960
"import org.bson.Document;\n",
6061
"import org.bson.conversions.Bson;\n",
@@ -82,7 +83,7 @@
8283
"source": [
8384
"## $match\n",
8485
"\n",
85-
"We'll get all book written after 2010"
86+
"We'll get all books written after 2010."
8687
]
8788
},
8889
{
@@ -96,13 +97,12 @@
9697
},
9798
"outputs": [],
9899
"source": [
99-
"List<Bson> booksFrom2010 = Arrays.asList(\n",
100-
" match(and(\n",
101-
" gte(\"year\", 2010)\n",
102-
" ))\n",
103-
");\n",
100+
"Bson yearFilter = gt(\"year\", 2010);\n",
101+
"Bson matchStage = match(yearFilter);\n",
104102
"\n",
105-
"AggregateIterable<Document> result = books.aggregate(booksFrom2010);\n",
103+
"List<Bson> aggregationPipeline = List.of(matchStage);\n",
104+
"\n",
105+
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
106106
"\n",
107107
"// Iterate through the results\n",
108108
"for (Document doc : result) {\n",
@@ -117,7 +117,7 @@
117117
"source": [
118118
"### $limit\n",
119119
"\n",
120-
"Now we want to limit just to 10 books"
120+
"We'll get books published after 2010 and limit the results to 10 documents."
121121
]
122122
},
123123
{
@@ -131,27 +131,33 @@
131131
},
132132
"outputs": [],
133133
"source": [
134-
"List<Bson> tenBooksFrom2010 = Arrays.asList(\n",
135-
" match(and(\n",
136-
" gte(\"year\", 2010)\n",
137-
" )),\n",
138-
" limit(10)\n",
139-
");\n",
134+
"Bson yearFilter = gt(\"year\", 2010);\n",
135+
"Bson matchStage = match(yearFilter);\n",
136+
"Bson limitStage = limit(10);\n",
140137
"\n",
141-
"AggregateIterable<Document> result = books.aggregate(tenBooksFrom2010);\n",
138+
"List<Bson> aggregationPipeline = List.of(matchStage, limitStage);\n",
139+
"\n",
140+
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
142141
"\n",
143-
"// Iterate through the results\n",
144142
"for (Document doc : result) {\n",
145143
" System.out.println(\"book: \" + doc.toJson());\n",
146144
"}"
147145
]
148146
},
147+
{
148+
"cell_type": "markdown",
149+
"id": "064ae5de",
150+
"metadata": {},
151+
"source": []
152+
},
149153
{
150154
"cell_type": "markdown",
151155
"id": "worth-windows",
152156
"metadata": {},
153157
"source": [
154-
"### $sort"
158+
"### $sort\n",
159+
"\n",
160+
"We'll sort the results by publication year in ascending order."
155161
]
156162
},
157163
{
@@ -165,17 +171,19 @@
165171
},
166172
"outputs": [],
167173
"source": [
168-
"List<Bson> tenBooksFrom2010 = Arrays.asList(\n",
169-
" match(and(\n",
170-
" gte(\"year\", 2010)\n",
171-
" )),\n",
172-
" limit(10),\n",
173-
" sort(new Document(\"year\", 1))\n",
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",
178+
"\n",
179+
"List<Bson> aggregationPipeline = List.of(\n",
180+
" matchStage,\n",
181+
" limitStage,\n",
182+
" sortStage\n",
174183
");\n",
175184
"\n",
176-
"AggregateIterable<Document> result = books.aggregate(tenBooksFrom2010);\n",
185+
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
177186
"\n",
178-
"// Iterate through the results\n",
179187
"for (Document doc : result) {\n",
180188
" System.out.println(\"book: \" + doc.toJson());\n",
181189
"}"

java/101_aggregation_pipeline_arrays.ipynb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,23 @@
4343
"import com.mongodb.client.MongoClients;\n",
4444
"import com.mongodb.client.MongoDatabase;\n",
4545
"import com.mongodb.client.MongoCollection;\n",
46-
"import com.mongodb.client.FindIterable;\n",
4746
"import com.mongodb.client.AggregateIterable;\n",
48-
"import com.mongodb.client.model.Accumulators;\n",
4947
"import com.mongodb.client.model.Projections;\n",
50-
"import com.mongodb.client.model.Filters;\n",
5148
"import com.mongodb.client.model.Field;\n",
5249
"import com.mongodb.client.model.Sorts;\n",
5350
"import com.mongodb.client.model.Aggregates;\n",
5451
"\n",
55-
"import static com.mongodb.client.model.Filters.gte;\n",
56-
"import static com.mongodb.client.model.Filters.and;\n",
5752
"import static com.mongodb.client.model.Filters.gt;\n",
5853
"import static com.mongodb.client.model.Filters.exists;\n",
54+
"import static com.mongodb.client.model.Filters.all;\n",
55+
"import static com.mongodb.client.model.Filters.in;\n",
56+
"import static com.mongodb.client.model.Sorts.descending;\n",
57+
"\n",
58+
"\n",
5959
"import static com.mongodb.client.model.Aggregates.match;\n",
60-
"import static com.mongodb.client.model.Aggregates.limit;\n",
60+
"import static com.mongodb.client.model.Aggregates.project;\n",
6161
"import static com.mongodb.client.model.Aggregates.sort;\n",
62+
"import static com.mongodb.client.model.Aggregates.addFields;\n",
6263
"\n",
6364
"import org.bson.Document;\n",
6465
"import org.bson.conversions.Bson;\n",
@@ -94,9 +95,10 @@
9495
"source": [
9596
"### $match: $all\n",
9697
"\n",
97-
"https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/using-arrays/simple-match-array \n",
98+
"[arrays-reference](\n",
99+
"https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/using-arrays/simple-match-array)\n",
98100
"\n",
99-
"If you want to search for all books that have \"Family Life\" and \"Fiction\", in any order (and possibly other genres) use:"
101+
"If you want to find books whose `genres` array contains both \"Family Life\" and \"Fiction\", in any order, use:"
100102
]
101103
},
102104
{
@@ -131,7 +133,7 @@
131133
"source": [
132134
"### $match: $in\n",
133135
"\n",
134-
"Finds books that have \"Family Life\" OR \"Fiction\""
136+
"Use `$in` to find books where the `genres` array contains at least one of the specified values."
135137
]
136138
},
137139
{

java/102_aggregation_pipeline_unwind.ipynb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@
4444
"import com.mongodb.client.MongoClients;\n",
4545
"import com.mongodb.client.MongoCollection;\n",
4646
"import com.mongodb.client.MongoDatabase;\n",
47-
"import com.mongodb.client.model.Accumulators;\n",
4847
"import com.mongodb.client.model.Aggregates;\n",
4948
"import com.mongodb.client.model.Filters;\n",
5049
"import org.bson.Document;\n",
5150
"\n",
52-
"import java.util.Arrays;\n",
53-
"\n",
5451
"// Set your connection String\n",
5552
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
5653
"\n",
@@ -67,12 +64,20 @@
6764
"MongoCollection<Document> books = library.getCollection(\"books\");"
6865
]
6966
},
67+
{
68+
"cell_type": "markdown",
69+
"id": "0b648872",
70+
"metadata": {},
71+
"source": []
72+
},
7073
{
7174
"cell_type": "markdown",
7275
"id": "handled-symbol",
7376
"metadata": {},
7477
"source": [
75-
"## $unwind"
78+
"## $unwind\n",
79+
"\n",
80+
"This pipeline first selects the book with `_id` `\"0004127382\"` and then uses `$unwind` to split the `attributes` array into separate documents. Each result contains the book title and one individual attribute.\n"
7681
]
7782
},
7883
{
@@ -86,13 +91,9 @@
8691
},
8792
"outputs": [],
8893
"source": [
89-
"AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n",
90-
" Aggregates.match(Filters.and(\n",
91-
" Filters.eq(\"_id\", \"60187778\")\n",
92-
" )),\n",
93-
"\n",
94-
" Aggregates.unwind(\"$attributes\"),\n",
95-
" \n",
94+
"AggregateIterable<Document> result = books.aggregate(List.of(\n",
95+
" Aggregates.match(Filters.eq(\"_id\", \"0004127382\")),\n",
96+
" Aggregates.unwind(\"$attributes\"), \n",
9697
" Aggregates.project(new Document()\n",
9798
" .append(\"title\", 1)\n",
9899
" .append(\"attributes\", 1)\n",

java/103_aggregation_pipeline_lookup.ipynb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"import static com.mongodb.client.model.Aggregates.limit;\n",
5353
"\n",
5454
"import org.bson.Document;\n",
55-
"import java.util.Arrays;\n",
5655
"\n",
5756
"// Set your connection String\n",
5857
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
@@ -75,7 +74,9 @@
7574
"id": "handled-symbol",
7675
"metadata": {},
7776
"source": [
78-
"## $lookup"
77+
"## $lookup\n",
78+
"\n",
79+
"This pipeline uses `$lookup` to join data from the `books` collection into each author document. It matches the values in the author's `books` array with the `_id` field in the `books` collection and stores the matching documents in a new field called `booksWritten`."
7980
]
8081
},
8182
{
@@ -90,7 +91,7 @@
9091
"outputs": [],
9192
"source": [
9293
"AggregateIterable<Document> result = authors.aggregate(\n",
93-
" Arrays.asList(\n",
94+
" List.of(\n",
9495
" lookup(\"books\", // collection with the book data to read into authors\n",
9596
" \"books\", // array in authors documents with the ids of all books written by an author\n",
9697
" \"_id\", // field in the books collection to link with\n",

java/104_aggregation_pipeline_group_by.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@
7070
"id": "handled-symbol",
7171
"metadata": {},
7272
"source": [
73-
"## $group"
73+
"## $group\n",
74+
"\n",
75+
"`$group` groups books by `year` and calculates the total number of pages for each group."
7476
]
7577
},
7678
{

0 commit comments

Comments
 (0)