Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 99 additions & 23 deletions java/100_aggregation_pipeline_match.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,39 @@
"outputs": [],
"source": [
"// Import the MongoDB Driver using Maven\n",
"%maven org.mongodb:mongodb-driver-sync:5.0.0\n",
" \n",
"%maven org.mongodb:mongodb-driver-sync:5.5.1\n",
"%maven org.slf4j:slf4j-simple:1.7.36\n",
"\n",
"import com.mongodb.client.MongoClient;\n",
"import com.mongodb.client.MongoClients;\n",
"import com.mongodb.client.MongoDatabase;\n",
"import com.mongodb.client.MongoCollection;\n",
"import com.mongodb.client.AggregateIterable;\n",
"\n",
"import com.mongodb.client.model.Aggregates;\n",
"import com.mongodb.client.model.Projections;\n",
"\n",
"import static com.mongodb.client.model.Filters.gt;\n",
"import static com.mongodb.client.model.Filters.eq;\n",
"import static com.mongodb.client.model.Projections.include;\n",
"import static com.mongodb.client.model.Projections.excludeId;\n",
"import static com.mongodb.client.model.Projections.fields;\n",
"\n",
"import com.mongodb.client.model.Aggregates;\n",
"import static com.mongodb.client.model.Aggregates.project;\n",
"import static com.mongodb.client.model.Aggregates.match;\n",
"import static com.mongodb.client.model.Aggregates.limit;\n",
"import static com.mongodb.client.model.Aggregates.sort;\n",
"import static com.mongodb.client.model.Sorts.ascending;\n",
"import static com.mongodb.client.model.Sorts.descending;\n",
"\n",
"import org.bson.Document;\n",
"import org.bson.conversions.Bson;\n",
"\n",
"import org.bson.Document;\n",
"import org.bson.conversions.Bson;\n",
"\n",
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
"\n",
"// Set your connection String\n",
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
"\n",
Expand All @@ -83,7 +93,7 @@
"source": [
"## $match\n",
"\n",
"We'll get all books written after 2010."
"Well use $match to return only the book with the title \"Treasure of the Sun\"."
]
},
{
Expand All @@ -97,9 +107,8 @@
},
"outputs": [],
"source": [
"Bson yearFilter = gt(\"year\", 2010);\n",
"Bson matchStage = match(yearFilter);\n",
"\n",
"Bson titleFilter = eq(\"title\", \"Treasure of the Sun\");\n",
"Bson matchStage = match(titleFilter);\n",
"List<Bson> aggregationPipeline = List.of(matchStage);\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
Expand All @@ -110,14 +119,55 @@
"}"
]
},
{
"cell_type": "markdown",
"id": "74f5fd5a",
"metadata": {},
"source": [
"## $project\n",
"\n",
"We’ll project only the `title` and `year` fields for the book \"Treasure of the Sun\", excluding the `_id` field from the result."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "50b8aa38",
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"Bson titleFilter = eq(\"title\", \"Treasure of the Sun\");\n",
"Bson matchStage = match(titleFilter);\n",
"\n",
"Bson projectStage = project(\n",
" fields(\n",
" include(\"title\", \"year\"),\n",
" excludeId()\n",
" )\n",
");\n",
"\n",
"List<Bson> aggregationPipeline = List.of(matchStage, projectStage);\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
" System.out.println(\"book: \" + doc.toJson());\n",
"}\n"
]
},
{
"cell_type": "markdown",
"id": "9f6caab3",
"metadata": {},
"source": [
"### $limit\n",
"## $limit\n",
"\n",
"We'll get books published after 2010 and limit the results to 10 documents."
"Well first match books published after 2010, then use `$limit` to return only 3 documents."
]
},
{
Expand All @@ -133,12 +183,13 @@
"source": [
"Bson yearFilter = gt(\"year\", 2010);\n",
"Bson matchStage = match(yearFilter);\n",
"Bson limitStage = limit(10);\n",
"Bson limitStage = limit(3);\n",
"\n",
"List<Bson> aggregationPipeline = List.of(matchStage, limitStage);\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
" System.out.println(\"book: \" + doc.toJson());\n",
"}"
Expand All @@ -155,9 +206,9 @@
"id": "worth-windows",
"metadata": {},
"source": [
"### $sort\n",
"## $sort\n",
"\n",
"We'll sort the results by publication year in ascending order."
"Well 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."
]
},
{
Expand All @@ -171,19 +222,25 @@
},
"outputs": [],
"source": [
"Bson yearFilter = gt(\"year\", 2010);\n",
"Bson matchStage = match(yearFilter);\n",
"Bson limitStage = limit(10);\n",
"Bson sortStage = sort(ascending(\"year\"));\n",
"Bson projectStage = project(\n",
" fields(\n",
" include(\"title\", \"pages\"),\n",
" excludeId()\n",
" )\n",
");\n",
"\n",
"Bson sortStage = sort(descending(\"pages\"));\n",
"Bson limitStage = limit(3);\n",
"\n",
"List<Bson> aggregationPipeline = List.of(\n",
" matchStage,\n",
" limitStage,\n",
" sortStage\n",
" projectStage,\n",
" sortStage,\n",
" limitStage\n",
");\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
" System.out.println(\"book: \" + doc.toJson());\n",
"}"
Expand Down Expand Up @@ -218,7 +275,15 @@
},
"outputs": [],
"source": [
"// type your code here"
"// TYPE YOUR CODE HERE\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(\n",
" List.of(<REPLACE_WITH_MATCH_STAGE>) \n",
");\n",
"\n",
"for (Document doc : result) {\n",
" System.out.println(\"book: \" + doc.toJson());\n",
"}"
]
},
{
Expand All @@ -242,7 +307,18 @@
},
"outputs": [],
"source": [
"// type your code here"
"// TYPE YOUR CODE HERE\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(\n",
" List.of(\n",
" <REPLACE_WITH_MATCH_STAGE>,\n",
" <REPLACE_WITH_PROJECT_STAGE>\n",
" )\n",
");\n",
"\n",
"for (Document doc : result) {\n",
" System.out.println(\"book: \" + doc.toJson());\n",
"}"
]
}
],
Expand Down
50 changes: 33 additions & 17 deletions java/101_aggregation_pipeline_arrays.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
"outputs": [],
"source": [
"// Import the MongoDB Driver using Maven\n",
"%maven org.mongodb:mongodb-driver-sync:5.0.0\n",
" \n",
"%maven org.mongodb:mongodb-driver-sync:5.5.1\n",
"%maven org.slf4j:slf4j-simple:1.7.36\n",
"\n",
"import com.mongodb.client.MongoClient;\n",
"import com.mongodb.client.MongoClients;\n",
"import com.mongodb.client.MongoDatabase;\n",
Expand All @@ -55,6 +56,9 @@
"import static com.mongodb.client.model.Filters.in;\n",
"import static com.mongodb.client.model.Sorts.descending;\n",
"\n",
"import static com.mongodb.client.model.Projections.include;\n",
"import static com.mongodb.client.model.Projections.excludeId;\n",
"import static com.mongodb.client.model.Projections.fields;\n",
"\n",
"import static com.mongodb.client.model.Aggregates.match;\n",
"import static com.mongodb.client.model.Aggregates.project;\n",
Expand All @@ -64,6 +68,10 @@
"import org.bson.Document;\n",
"import org.bson.conversions.Bson;\n",
"\n",
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
"\n",
"// Set your connection String\n",
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
"\n",
Expand Down Expand Up @@ -93,12 +101,12 @@
"id": "handled-symbol",
"metadata": {},
"source": [
"### $match: $all\n",
"### $match with the `$all` operator\n",
"\n",
"[arrays-reference](\n",
"https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/using-arrays/simple-match-array)\n",
"\n",
"If you want to find books whose `genres` array contains both \"Family Life\" and \"Fiction\", in any order, use:"
"If you want to find books whose `genres` array contains both `\"Family Life\"` and `\"Fiction\"`, in any order, use:"
]
},
{
Expand All @@ -112,13 +120,17 @@
},
"outputs": [],
"source": [
"AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n",
" Aggregates.match(Filters.all(\"genres\", \"Family Life\", \"Fiction\")),\n",
" Aggregates.project(new Document()\n",
" .append(\"title\", 1)\n",
" .append(\"genres\", 1)\n",
"AggregateIterable<Document> result = books.aggregate(\n",
" List.of(\n",
" match(all(\"genres\", \"Family Life\", \"Fiction\")),\n",
" project(\n",
" fields(\n",
" include(\"title\", \"genres\"),\n",
" excludeId()\n",
" )\n",
" )\n",
" )\n",
"));\n",
");\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
Expand All @@ -131,7 +143,7 @@
"id": "9f6caab3",
"metadata": {},
"source": [
"### $match: $in\n",
"### $match with the `$in` operator\n",
"\n",
"Use `$in` to find books where the `genres` array contains at least one of the specified values."
]
Expand All @@ -147,13 +159,17 @@
},
"outputs": [],
"source": [
"AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n",
" Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n",
" Aggregates.project(new Document()\n",
" .append(\"title\", 1)\n",
" .append(\"genres\", 1)\n",
"// Note: `Document` can also be used here because it implements `Bson`.\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(\n",
" List.of(\n",
" Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n",
" Aggregates.project(new Document()\n",
" .append(\"title\", 1)\n",
" .append(\"genres\", 1)\n",
" )\n",
" )\n",
"));\n",
");\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
Expand Down
33 changes: 23 additions & 10 deletions java/102_aggregation_pipeline_unwind.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
"outputs": [],
"source": [
"// Import the MongoDB Driver using Maven\n",
"%maven org.mongodb:mongodb-driver-sync:5.0.0\n",
" \n",
"%maven org.mongodb:mongodb-driver-sync:5.5.1\n",
"%maven org.slf4j:slf4j-simple:1.7.36\n",
"\n",
"import com.mongodb.client.AggregateIterable;\n",
"import com.mongodb.client.MongoClient;\n",
"import com.mongodb.client.MongoClients;\n",
Expand All @@ -48,6 +49,19 @@
"import com.mongodb.client.model.Filters;\n",
"import org.bson.Document;\n",
"\n",
"import static com.mongodb.client.model.Filters.eq;\n",
"import static com.mongodb.client.model.Projections.include;\n",
"import static com.mongodb.client.model.Projections.excludeId;\n",
"import static com.mongodb.client.model.Projections.fields;\n",
"\n",
"import static com.mongodb.client.model.Aggregates.match;\n",
"import static com.mongodb.client.model.Aggregates.project;\n",
"import static com.mongodb.client.model.Aggregates.unwind;\n",
"\n",
"// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n",
"System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n",
"System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n",
"\n",
"// Set your connection String\n",
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
"\n",
Expand Down Expand Up @@ -77,7 +91,7 @@
"source": [
"## $unwind\n",
"\n",
"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"
"This pipeline first selects the book with `_id` `\"0004127382\"`, then uses `$unwind` to split the `attributes` array into separate documents, and finally uses `$project` to return only the `title` and `attributes` fields."
]
},
{
Expand All @@ -91,14 +105,13 @@
},
"outputs": [],
"source": [
"AggregateIterable<Document> result = books.aggregate(List.of(\n",
" Aggregates.match(Filters.eq(\"_id\", \"0004127382\")),\n",
" Aggregates.unwind(\"$attributes\"), \n",
" Aggregates.project(new Document()\n",
" .append(\"title\", 1)\n",
" .append(\"attributes\", 1)\n",
"AggregateIterable<Document> result = books.aggregate(\n",
" List.of(\n",
" match(eq(\"_id\", \"0004127382\")),\n",
" unwind(\"$attributes\"),\n",
" project(fields(include(\"title\", \"attributes\"), excludeId()))\n",
" )\n",
"));\n",
");\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
Expand Down
Loading
Loading