|
56 | 56 | "import static com.mongodb.client.model.Filters.in;\n", |
57 | 57 | "import static com.mongodb.client.model.Sorts.descending;\n", |
58 | 58 | "\n", |
| 59 | + "import static com.mongodb.client.model.Projections.include;\n", |
| 60 | + "import static com.mongodb.client.model.Projections.excludeId;\n", |
| 61 | + "import static com.mongodb.client.model.Projections.fields;\n", |
59 | 62 | "\n", |
60 | 63 | "import static com.mongodb.client.model.Aggregates.match;\n", |
61 | 64 | "import static com.mongodb.client.model.Aggregates.project;\n", |
|
66 | 69 | "import org.bson.conversions.Bson;\n", |
67 | 70 | "\n", |
68 | 71 | "// Configure SLF4J Simple Logger to suppress MongoDB driver logs in the notebook output.\n", |
69 | | - "\n", |
70 | 72 | "System.setProperty(\"org.slf4j.simpleLogger.defaultLogLevel\", \"off\");\n", |
71 | 73 | "System.setProperty(\"org.slf4j.simpleLogger.log.org.mongodb.driver\", \"off\");\n", |
72 | 74 | "\n", |
|
118 | 120 | }, |
119 | 121 | "outputs": [], |
120 | 122 | "source": [ |
121 | | - "AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n", |
122 | | - " Aggregates.match(Filters.all(\"genres\", \"Family Life\", \"Fiction\")),\n", |
123 | | - " Aggregates.project(new Document()\n", |
124 | | - " .append(\"title\", 1)\n", |
125 | | - " .append(\"genres\", 1)\n", |
| 123 | + "AggregateIterable<Document> result = books.aggregate(\n", |
| 124 | + " List.of(\n", |
| 125 | + " match(all(\"genres\", \"Family Life\", \"Fiction\")),\n", |
| 126 | + " project(\n", |
| 127 | + " fields(\n", |
| 128 | + " include(\"title\", \"genres\"),\n", |
| 129 | + " excludeId()\n", |
| 130 | + " )\n", |
| 131 | + " )\n", |
126 | 132 | " )\n", |
127 | | - "));\n", |
| 133 | + ");\n", |
128 | 134 | "\n", |
129 | | - "// Iterate through the results\n", |
130 | 135 | "for (Document doc : result) {\n", |
131 | 136 | " System.out.println(\"book: \" + doc.toJson());\n", |
132 | 137 | "}" |
|
137 | 142 | "id": "9f6caab3", |
138 | 143 | "metadata": {}, |
139 | 144 | "source": [ |
140 | | - "### $match: $in\n", |
| 145 | + "### $match with the `$in` operator\n", |
141 | 146 | "\n", |
142 | 147 | "Use `$in` to find books where the `genres` array contains at least one of the specified values." |
143 | 148 | ] |
|
153 | 158 | }, |
154 | 159 | "outputs": [], |
155 | 160 | "source": [ |
156 | | - "AggregateIterable<Document> result = books.aggregate(Arrays.asList(\n", |
157 | | - " Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n", |
158 | | - " Aggregates.project(new Document()\n", |
159 | | - " .append(\"title\", 1)\n", |
160 | | - " .append(\"genres\", 1)\n", |
| 161 | + "// Note: `Document` can also be used here because it implements `Bson`.\n", |
| 162 | + "\n", |
| 163 | + "AggregateIterable<Document> result = books.aggregate(\n", |
| 164 | + " List.of(\n", |
| 165 | + " Aggregates.match(Filters.in(\"genres\", \"Family Life\", \"Fiction\")),\n", |
| 166 | + " Aggregates.project(new Document()\n", |
| 167 | + " .append(\"title\", 1)\n", |
| 168 | + " .append(\"genres\", 1)\n", |
| 169 | + " )\n", |
161 | 170 | " )\n", |
162 | | - "));\n", |
| 171 | + ");\n", |
163 | 172 | "\n", |
164 | | - "// Iterate through the results\n", |
165 | 173 | "for (Document doc : result) {\n", |
166 | 174 | " System.out.println(\"book: \" + doc.toJson());\n", |
167 | 175 | "}" |
|
0 commit comments