|
49 | 49 | "import com.mongodb.client.model.Filters;\n", |
50 | 50 | "import org.bson.Document;\n", |
51 | 51 | "\n", |
52 | | - "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\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", |
| 56 | + "\n", |
| 57 | + "import static com.mongodb.client.model.Aggregates.match;\n", |
| 58 | + "import static com.mongodb.client.model.Aggregates.project;\n", |
| 59 | + "import static com.mongodb.client.model.Aggregates.unwind;\n", |
53 | 60 | "\n", |
| 61 | + "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", |
54 | 62 | "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", |
55 | 63 | "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", |
56 | 64 | "\n", |
|
83 | 91 | "source": [ |
84 | 92 | "## $unwind\n", |
85 | 93 | "\n", |
86 | | - "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" |
| 94 | + "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." |
87 | 95 | ] |
88 | 96 | }, |
89 | 97 | { |
|
97 | 105 | }, |
98 | 106 | "outputs": [], |
99 | 107 | "source": [ |
100 | | - "AggregateIterable<Document> result = books.aggregate(List.of(\n", |
101 | | - " Aggregates.match(Filters.eq(\"_id\", \"0004127382\")),\n", |
102 | | - " Aggregates.unwind(\"$attributes\"), \n", |
103 | | - " Aggregates.project(new Document()\n", |
104 | | - " .append(\"title\", 1)\n", |
105 | | - " .append(\"attributes\", 1)\n", |
| 108 | + "AggregateIterable<Document> result = books.aggregate(\n", |
| 109 | + " List.of(\n", |
| 110 | + " match(eq(\"_id\", \"0004127382\")),\n", |
| 111 | + " unwind(\"$attributes\"),\n", |
| 112 | + " project(fields(include(\"title\", \"attributes\"), excludeId()))\n", |
106 | 113 | " )\n", |
107 | | - "));\n", |
| 114 | + ");\n", |
108 | 115 | "\n", |
109 | 116 | "// Iterate through the results\n", |
110 | 117 | "for (Document doc : result) {\n", |
|
0 commit comments