@@ -207,22 +207,7 @@ class CountryQueryService {
207
207
});
208
208
}
209
209
210
- // --- Stage 4: Project to original Country structure and ensure uniqueness ---
211
- // After lookups and matches, we might have duplicate countries if they
212
- // matched multiple sources/headlines. We need to group them back to unique countries.
213
- pipeline.add ({
214
- r'$group' : {
215
- '_id' : r'$_id' , // Group by the original country ID
216
- 'doc' : {r'$first' : r'$$ROOT' }, // Take the first full document
217
- },
218
- });
219
- pipeline.add ({
220
- r'$replaceRoot' : {
221
- 'newRoot' : r'$doc' , // Replace root with the original document
222
- },
223
- });
224
-
225
- // --- Stage 5: Sorting ---
210
+ // --- Stage 4: Sorting ---
226
211
if (sort != null && sort.isNotEmpty) {
227
212
final sortStage = < String , dynamic > {};
228
213
for (final option in sort) {
@@ -231,7 +216,7 @@ class CountryQueryService {
231
216
pipeline.add ({r'$sort' : sortStage});
232
217
}
233
218
234
- // --- Stage 6 : Pagination (Skip and Limit) ---
219
+ // --- Stage 5 : Pagination (Skip and Limit) ---
235
220
if (pagination? .cursor != null ) {
236
221
// For cursor-based pagination, we'd typically need a more complex
237
222
// aggregation that sorts by the cursor field and then skips.
@@ -247,9 +232,11 @@ class CountryQueryService {
247
232
pipeline.add ({r'$limit' : pagination! .limit! + 1 });
248
233
}
249
234
250
- // --- Stage 7: Final Projection ---
251
- // Project to match the Country model's JSON structure if necessary
252
- // (e.g., if _id was used, map it back to id)
235
+ // --- Stage 6: Final Projection ---
236
+ // Project to match the Country model's JSON structure.
237
+ // The $lookup stages add fields ('matchingSources', 'matchingHeadlines')
238
+ // that are not part of the Country model, so we project only the fields
239
+ // that are part of the model to ensure clean deserialization.
253
240
pipeline.add ({
254
241
r'$project' : {
255
242
'_id' : 0 , // Exclude _id
@@ -260,7 +247,6 @@ class CountryQueryService {
260
247
'createdAt' : r'$createdAt' ,
261
248
'updatedAt' : r'$updatedAt' ,
262
249
'status' : r'$status' ,
263
- // Ensure other fields are projected if they were modified or needed
264
250
},
265
251
});
266
252
0 commit comments