|
43 | 43 | "import com.mongodb.client.MongoClients;\n", |
44 | 44 | "import com.mongodb.client.MongoDatabase;\n", |
45 | 45 | "import com.mongodb.client.MongoCollection;\n", |
46 | | - "import com.mongodb.client.FindIterable;\n", |
47 | 46 | "import com.mongodb.client.AggregateIterable;\n", |
48 | 47 | "\n", |
49 | 48 | "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", |
52 | 49 | "import static com.mongodb.client.model.Filters.gt;\n", |
53 | 50 | "\n", |
54 | 51 | "import com.mongodb.client.model.Aggregates;\n", |
55 | 52 | "import static com.mongodb.client.model.Aggregates.match;\n", |
56 | 53 | "import static com.mongodb.client.model.Aggregates.limit;\n", |
57 | 54 | "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", |
58 | 59 | "\n", |
59 | 60 | "import org.bson.Document;\n", |
60 | 61 | "import org.bson.conversions.Bson;\n", |
|
82 | 83 | "source": [ |
83 | 84 | "## $match\n", |
84 | 85 | "\n", |
85 | | - "We'll get all book written after 2010" |
| 86 | + "We'll get all books written after 2010." |
86 | 87 | ] |
87 | 88 | }, |
88 | 89 | { |
|
96 | 97 | }, |
97 | 98 | "outputs": [], |
98 | 99 | "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", |
104 | 102 | "\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", |
106 | 106 | "\n", |
107 | 107 | "// Iterate through the results\n", |
108 | 108 | "for (Document doc : result) {\n", |
|
117 | 117 | "source": [ |
118 | 118 | "### $limit\n", |
119 | 119 | "\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." |
121 | 121 | ] |
122 | 122 | }, |
123 | 123 | { |
|
131 | 131 | }, |
132 | 132 | "outputs": [], |
133 | 133 | "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", |
140 | 137 | "\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", |
142 | 141 | "\n", |
143 | | - "// Iterate through the results\n", |
144 | 142 | "for (Document doc : result) {\n", |
145 | 143 | " System.out.println(\"book: \" + doc.toJson());\n", |
146 | 144 | "}" |
147 | 145 | ] |
148 | 146 | }, |
| 147 | + { |
| 148 | + "cell_type": "markdown", |
| 149 | + "id": "064ae5de", |
| 150 | + "metadata": {}, |
| 151 | + "source": [] |
| 152 | + }, |
149 | 153 | { |
150 | 154 | "cell_type": "markdown", |
151 | 155 | "id": "worth-windows", |
152 | 156 | "metadata": {}, |
153 | 157 | "source": [ |
154 | | - "### $sort" |
| 158 | + "### $sort\n", |
| 159 | + "\n", |
| 160 | + "We'll sort the results by publication year in ascending order." |
155 | 161 | ] |
156 | 162 | }, |
157 | 163 | { |
|
165 | 171 | }, |
166 | 172 | "outputs": [], |
167 | 173 | "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", |
174 | 183 | ");\n", |
175 | 184 | "\n", |
176 | | - "AggregateIterable<Document> result = books.aggregate(tenBooksFrom2010);\n", |
| 185 | + "AggregateIterable<Document> result = books.aggregate(aggregationPipeline);\n", |
177 | 186 | "\n", |
178 | | - "// Iterate through the results\n", |
179 | 187 | "for (Document doc : result) {\n", |
180 | 188 | " System.out.println(\"book: \" + doc.toJson());\n", |
181 | 189 | "}" |
|
0 commit comments