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
64 changes: 36 additions & 28 deletions java/100_aggregation_pipeline_match.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@
"import com.mongodb.client.MongoClients;\n",
"import com.mongodb.client.MongoDatabase;\n",
"import com.mongodb.client.MongoCollection;\n",
"import com.mongodb.client.FindIterable;\n",
"import com.mongodb.client.AggregateIterable;\n",
"\n",
"import com.mongodb.client.model.Projections;\n",
"import static com.mongodb.client.model.Filters.gte;\n",
"import static com.mongodb.client.model.Filters.and;\n",
"import static com.mongodb.client.model.Filters.gt;\n",
"\n",
"import com.mongodb.client.model.Aggregates;\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",
"\n",
"import org.bson.Document;\n",
"import org.bson.conversions.Bson;\n",
"\n",
"import org.bson.Document;\n",
"import org.bson.conversions.Bson;\n",
Expand Down Expand Up @@ -82,7 +83,7 @@
"source": [
"## $match\n",
"\n",
"We'll get all book written after 2010"
"We'll get all books written after 2010."
]
},
{
Expand All @@ -96,13 +97,12 @@
},
"outputs": [],
"source": [
"List<Bson> booksFrom2010 = Arrays.asList(\n",
" match(and(\n",
" gte(\"year\", 2010)\n",
" ))\n",
");\n",
"Bson yearFilter = gt(\"year\", 2010);\n",
"Bson matchStage = match(yearFilter);\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(booksFrom2010);\n",
"List<Bson> aggregationPipeline = List.of(matchStage);\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n",
"\n",
"// Iterate through the results\n",
"for (Document doc : result) {\n",
Expand All @@ -117,7 +117,7 @@
"source": [
"### $limit\n",
"\n",
"Now we want to limit just to 10 books"
"We'll get books published after 2010 and limit the results to 10 documents."
]
},
{
Expand All @@ -131,27 +131,33 @@
},
"outputs": [],
"source": [
"List<Bson> tenBooksFrom2010 = Arrays.asList(\n",
" match(and(\n",
" gte(\"year\", 2010)\n",
" )),\n",
" limit(10)\n",
");\n",
"Bson yearFilter = gt(\"year\", 2010);\n",
"Bson matchStage = match(yearFilter);\n",
"Bson limitStage = limit(10);\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(tenBooksFrom2010);\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",
"}"
]
},
{
"cell_type": "markdown",
"id": "064ae5de",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "worth-windows",
"metadata": {},
"source": [
"### $sort"
"### $sort\n",
"\n",
"We'll sort the results by publication year in ascending order."
]
},
{
Expand All @@ -165,17 +171,19 @@
},
"outputs": [],
"source": [
"List<Bson> tenBooksFrom2010 = Arrays.asList(\n",
" match(and(\n",
" gte(\"year\", 2010)\n",
" )),\n",
" limit(10),\n",
" sort(new Document(\"year\", 1))\n",
"Bson yearFilter = gt(\"year\", 2010);\n",
"Bson matchStage = match(yearFilter);\n",
"Bson limitStage = limit(10);\n",
"Bson sortStage = sort(ascending(\"year\"));\n",
"\n",
"List<Bson> aggregationPipeline = List.of(\n",
" matchStage,\n",
" limitStage,\n",
" sortStage\n",
");\n",
"\n",
"AggregateIterable<Document> result = books.aggregate(tenBooksFrom2010);\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
20 changes: 11 additions & 9 deletions java/101_aggregation_pipeline_arrays.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,23 @@
"import com.mongodb.client.MongoClients;\n",
"import com.mongodb.client.MongoDatabase;\n",
"import com.mongodb.client.MongoCollection;\n",
"import com.mongodb.client.FindIterable;\n",
"import com.mongodb.client.AggregateIterable;\n",
"import com.mongodb.client.model.Accumulators;\n",
"import com.mongodb.client.model.Projections;\n",
"import com.mongodb.client.model.Filters;\n",
"import com.mongodb.client.model.Field;\n",
"import com.mongodb.client.model.Sorts;\n",
"import com.mongodb.client.model.Aggregates;\n",
"\n",
"import static com.mongodb.client.model.Filters.gte;\n",
"import static com.mongodb.client.model.Filters.and;\n",
"import static com.mongodb.client.model.Filters.gt;\n",
"import static com.mongodb.client.model.Filters.exists;\n",
"import static com.mongodb.client.model.Filters.all;\n",
"import static com.mongodb.client.model.Filters.in;\n",
"import static com.mongodb.client.model.Sorts.descending;\n",
"\n",
"\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.project;\n",
"import static com.mongodb.client.model.Aggregates.sort;\n",
"import static com.mongodb.client.model.Aggregates.addFields;\n",
"\n",
"import org.bson.Document;\n",
"import org.bson.conversions.Bson;\n",
Expand Down Expand Up @@ -94,9 +95,10 @@
"source": [
"### $match: $all\n",
"\n",
"https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/using-arrays/simple-match-array \n",
"[arrays-reference](\n",
"https://mongodb-developer.github.io/aggregation-pipeline-lab/docs/using-arrays/simple-match-array)\n",
"\n",
"If you want to search for all books that have \"Family Life\" and \"Fiction\", in any order (and possibly other genres) use:"
"If you want to find books whose `genres` array contains both \"Family Life\" and \"Fiction\", in any order, use:"
]
},
{
Expand Down Expand Up @@ -131,7 +133,7 @@
"source": [
"### $match: $in\n",
"\n",
"Finds books that have \"Family Life\" OR \"Fiction\""
"Use `$in` to find books where the `genres` array contains at least one of the specified values."
]
},
{
Expand Down
23 changes: 12 additions & 11 deletions java/102_aggregation_pipeline_unwind.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@
"import com.mongodb.client.MongoClients;\n",
"import com.mongodb.client.MongoCollection;\n",
"import com.mongodb.client.MongoDatabase;\n",
"import com.mongodb.client.model.Accumulators;\n",
"import com.mongodb.client.model.Aggregates;\n",
"import com.mongodb.client.model.Filters;\n",
"import org.bson.Document;\n",
"\n",
"import java.util.Arrays;\n",
"\n",
"// Set your connection String\n",
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
"\n",
Expand All @@ -67,12 +64,20 @@
"MongoCollection<Document> books = library.getCollection(\"books\");"
]
},
{
"cell_type": "markdown",
"id": "0b648872",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "handled-symbol",
"metadata": {},
"source": [
"## $unwind"
"## $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"
]
},
{
Expand All @@ -86,13 +91,9 @@
},
"outputs": [],
"source": [
"AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n",
" Aggregates.match(Filters.and(\n",
" Filters.eq(\"_id\", \"60187778\")\n",
" )),\n",
"\n",
" Aggregates.unwind(\"$attributes\"),\n",
" \n",
"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",
Expand Down
7 changes: 4 additions & 3 deletions java/103_aggregation_pipeline_lookup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"import static com.mongodb.client.model.Aggregates.limit;\n",
"\n",
"import org.bson.Document;\n",
"import java.util.Arrays;\n",
"\n",
"// Set your connection String\n",
"String connectionString = \"mongodb://admin:mongodb@localhost:27017/\";\n",
Expand All @@ -75,7 +74,9 @@
"id": "handled-symbol",
"metadata": {},
"source": [
"## $lookup"
"## $lookup\n",
"\n",
"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`."
]
},
{
Expand All @@ -90,7 +91,7 @@
"outputs": [],
"source": [
"AggregateIterable<Document> result = authors.aggregate(\n",
" Arrays.asList(\n",
" List.of(\n",
" lookup(\"books\", // collection with the book data to read into authors\n",
" \"books\", // array in authors documents with the ids of all books written by an author\n",
" \"_id\", // field in the books collection to link with\n",
Expand Down
4 changes: 3 additions & 1 deletion java/104_aggregation_pipeline_group_by.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
"id": "handled-symbol",
"metadata": {},
"source": [
"## $group"
"## $group\n",
"\n",
"`$group` groups books by `year` and calculates the total number of pages for each group."
]
},
{
Expand Down
Loading